Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up integration tests #6

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ concurrency:
cancel-in-progress: true

jobs:
test:
uses: "./.github/workflows/ci_test.yml"
unit-test:
uses: "./.github/workflows/ci_unit_test.yml"
secrets: inherit

integration-test:
uses: "./.github/workflows/ci_integration_test.yml"
secrets: inherit

lint:
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/ci_integration_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: integration-test

on:
workflow_call:

jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
go:
- '1.18'

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
cache: true

- env:
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/...
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test
name: unit-test

on:
workflow_call:
Expand Down
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
9 changes: 7 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ tasks:
- ./.golangci.yml
method: checksum

unittest:
unit-test:
desc: Runs Golang unit tests
cmds:
- go test -v ./...
- go test -v ./...

integration-test:
desc: Runs Golang integration tests
cmds:
- go test -tags=integration ./tests/integration/...
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func NewLoggingClientOption() api.ClientOption {
}

func newSDKVersionHeaderEditorFn(_ context.Context, req *http.Request) error {
req.Header.Set("X-Corbado-SDK-Version", fmt.Sprintf("Go %s", Version))
req.Header.Set("X-Corbado-SDK-Version", fmt.Sprintf("Go SDK %s", Version))

return nil
}
2 changes: 1 addition & 1 deletion sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/pkg/errors"
)

const Version = "v0.1.0"
const Version = "v0.4.0"

type SDK interface {
AuthTokens() authtoken.AuthToken
Expand Down
28 changes: 28 additions & 0 deletions tests/integration/emaillink/email_link_send_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build integration

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) {
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,
AdditionalPayload: util.Ptr("{}"),
ClientInfo: util.ClientInfo("foobar", "127.0.0.1"),
})

require.NoError(t, err)
assert.NotEmpty(t, rsp.Data.EmailLinkID)
}
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)
}
93 changes: 93 additions & 0 deletions tests/integration/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//go:build integration

package integration

import (
"crypto/rand"
"math/big"
"os"
"runtime"
"strings"
"testing"

"github.com/corbado/corbado-go"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func SDK(t *testing.T) corbado.SDK {
config, err := corbado.NewConfig(GetProjectID(t), GetAPISecret(t))
require.NoError(t, err)
config.BackendAPI = GetBackendAPI(t)

sdk, err := corbado.NewSDK(config)
require.NoError(t, err)

return sdk
}

func getEnv(t *testing.T, name string) string {
env := os.Getenv(name)
if env == "" {
t.Fatalf("Missing env variable %s", name)
}

return env
}

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"

res := make([]byte, length)
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return "", errors.WithStack(err)
}

res[i] = letters[num.Int64()]
}

return string(res), nil
}

func getFunctionName() string {
pc := make([]uintptr, 15)
n := runtime.Callers(3, pc)

frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()

functionName := frame.Function
functionName = functionName[strings.LastIndex(functionName, ".")+1:]
functionName = functionName[5:]

return functionName
}
Loading