Skip to content

Commit

Permalink
Move tests into packages
Browse files Browse the repository at this point in the history
  • Loading branch information
corbadovych committed Nov 20, 2023
1 parent ed88c58 commit 2c4fed1
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
CORBADO_BACKEND_API: ${{ secrets.CORBADO_BACKEND_API }}
CORBADO_PROJECT_ID: ${{ secrets.CORBADO_PROJECT_ID }}
CORBADO_API_SECRET: ${{ secrets.CORBADO_API_SECRET }}
run: go test -tags=integration ./tests/integration
run: go test -tags=integration ./tests/integration/...
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The SDK supports Go version 1.18 and above.
## Usage

```
$ go get github.com/corbado/corbado-go@v0.1.0
$ go get github.com/corbado/corbado-go@v0.4.0
```

Import SDK in your Go files:
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ tasks:
integration-test:
desc: Runs Golang integration tests
cmds:
- go test -tags=integration ./tests/integration
- go test -tags=integration ./tests/integration/...
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//go:build integration

package integration_test
package emaillink_test

import (
"context"
"testing"

"github.com/corbado/corbado-go/pkg/sdk/entity/api"
"github.com/corbado/corbado-go/pkg/sdk/util"
"github.com/corbado/corbado-go/tests/integration"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEmailLinkSend(t *testing.T) {
sdk := initClient(t)
rsp, err := sdk.EmailLinks().Send(context.TODO(), api.EmailLinkSendReq{
Email: createRandomTestEmail(t),
rsp, err := integration.SDK(t).EmailLinks().Send(context.TODO(), api.EmailLinkSendReq{
Email: integration.CreateRandomTestEmail(t),
Redirect: "https://some.site.com/authenticate",
TemplateName: util.Ptr("default"),
Create: true,
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/project/project_config_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build integration

package project_test

import (
"context"
"testing"

"github.com/corbado/corbado-go/tests/integration"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestProjectConfigGet(t *testing.T) {
rsp, err := integration.SDK(t).Projects().ConfigGet(context.TODO())

require.NoError(t, err)
assert.Equal(t, integration.GetProjectID(t), rsp.Data.ProjectID)
}
26 changes: 26 additions & 0 deletions tests/integration/project/project_config_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build integration

package project_test

import (
"context"
"testing"

"github.com/corbado/corbado-go/pkg/sdk/entity/api"
"github.com/corbado/corbado-go/pkg/sdk/util"
"github.com/corbado/corbado-go/tests/integration"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestProjectConfigUpdate(t *testing.T) {
newName := integration.CreateRandomTestName(t)
err := integration.SDK(t).Projects().ConfigUpdate(context.TODO(), api.ProjectConfigSaveReq{
ExternalName: util.Ptr(newName),
})
require.NoError(t, err)

newCfg, err := integration.SDK(t).Projects().ConfigGet(context.TODO())
require.NoError(t, err)
assert.Equal(t, newName, newCfg.Data.ExternalName)
}
32 changes: 32 additions & 0 deletions tests/integration/user/user_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build integration

package user_test

import (
"context"
"testing"

"github.com/corbado/corbado-go/pkg/sdk/entity/api"
"github.com/corbado/corbado-go/pkg/sdk/util"
"github.com/corbado/corbado-go/tests/integration"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestUserList(t *testing.T) {
// send email link first so that we have at least one user
_, err := integration.SDK(t).EmailLinks().Send(context.TODO(), api.EmailLinkSendReq{
Email: integration.CreateRandomTestEmail(t),
Redirect: "https://some.site.com/authenticate",
TemplateName: util.Ptr("default"),
Create: true,
AdditionalPayload: util.Ptr("{}"),
ClientInfo: util.ClientInfo("foobar", "127.0.0.1"),
})
require.NoError(t, err)

usersRsp, err := integration.SDK(t).Users().List(context.TODO(), nil)
require.NoError(t, err)

assert.True(t, len(usersRsp.Data.Users) > 0)
}
29 changes: 24 additions & 5 deletions tests/integration/corbado_test.go → tests/integration/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build integration

package integration_test
package integration

import (
"crypto/rand"
Expand All @@ -15,10 +15,10 @@ import (
"github.com/stretchr/testify/require"
)

func initClient(t *testing.T) corbado.SDK {
config, err := corbado.NewConfig(getEnv(t, "CORBADO_PROJECT_ID"), getEnv(t, "CORBADO_API_SECRET"))
func SDK(t *testing.T) corbado.SDK {
config, err := corbado.NewConfig(GetProjectID(t), GetAPISecret(t))
require.NoError(t, err)
config.BackendAPI = getEnv(t, "CORBADO_BACKEND_API")
config.BackendAPI = GetBackendAPI(t)

sdk, err := corbado.NewSDK(config)
require.NoError(t, err)
Expand All @@ -35,13 +35,32 @@ func getEnv(t *testing.T, name string) string {
return env
}

func createRandomTestEmail(t *testing.T) string {
func GetProjectID(t *testing.T) string {
return getEnv(t, "CORBADO_PROJECT_ID")
}

func GetAPISecret(t *testing.T) string {
return getEnv(t, "CORBADO_API_SECRET")
}

func GetBackendAPI(t *testing.T) string {
return getEnv(t, "CORBADO_BACKEND_API")
}

func CreateRandomTestEmail(t *testing.T) string {
value, err := generateString(10)
require.NoError(t, err)

return getFunctionName() + value + "@test.de"
}

func CreateRandomTestName(t *testing.T) string {
value, err := generateString(10)
require.NoError(t, err)

return value
}

func generateString(length int) (string, error) {
// Removed I, 1, 0 and O because of risk of confusion
const letters = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijklmnopwrstuvwxyz23456789"
Expand Down

0 comments on commit 2c4fed1

Please sign in to comment.