Skip to content

Commit

Permalink
Separate integration tests from unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyTitu committed Jan 22, 2024
1 parent 4eaca06 commit 8464909
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 200 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ func main() {
}
// do something with the secret
}
```
To pass the service account token as an environment variable (`OP_SERVICE_ACCOUNT_TOKEN`), you can also use the `onepassword.NewServiceAccountClientFromEnv()` function.
```
105 changes: 98 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,112 @@
package onepassword

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"
)

// invalid name/version test cases may belong in integration_test.go
// currently these tests fail correctly, but they have incorrect tokens, so the accuracy of these fails is in question

func TestNoToken(t *testing.T) {
clientFactory, err := NewClientFactory(context.TODO())
require.NoError(t, err)
clientFactory := NewTestClientFactory()

// missing token
_, err = clientFactory.NewClient(
_, err := clientFactory.NewClient(
WithIntegrationInfo(DefaultIntegrationName, DefaultIntegrationVersion))
require.Error(t, err)
}

func TestNoIntegrationNameOrVersion(t *testing.T) {
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")

clientFactory := NewTestClientFactory()

_, err := clientFactory.NewClient(
WithServiceAccountToken(token),
WithIntegrationInfo("", ""))
require.Error(t, err)
}

func TestNoIntegrationName(t *testing.T) {
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")

clientFactory := NewTestClientFactory()

_, err := clientFactory.NewClient(
WithServiceAccountToken(token),
WithIntegrationInfo("", DefaultIntegrationVersion))
require.Error(t, err)
}

func TestInvalidIntegrationNameLength(t *testing.T) {
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")

clientFactory := NewTestClientFactory()

_, err := clientFactory.NewClient(
WithServiceAccountToken(token),
WithIntegrationInfo("12345678901234567890123456789012345678901234567890", DefaultIntegrationVersion))
require.Error(t, err)
}

func TestInvalidIntegrationNameCharacters(t *testing.T) {
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")

clientFactory := NewTestClientFactory()

_, err := clientFactory.NewClient(
WithServiceAccountToken(token),
WithIntegrationInfo("$", DefaultIntegrationVersion))
require.Error(t, err)
}

func TestNoIntegrationVersion(t *testing.T) {
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")

clientFactory := NewTestClientFactory()

_, err := clientFactory.NewClient(
WithServiceAccountToken(token),
WithIntegrationInfo(DefaultIntegrationName, ""))
require.Error(t, err)
}

func TestInvalidIntegrationVersionLength(t *testing.T) {
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")

clientFactory := NewTestClientFactory()

_, err := clientFactory.NewClient(
WithServiceAccountToken(token),
WithIntegrationInfo(DefaultIntegrationName, "12345678901234567890123456789012345678901234567890"))
require.Error(t, err)
}

func TestInvalidIntegrationVersionCharacters(t *testing.T) {
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")

clientFactory := NewTestClientFactory()

_, err := clientFactory.NewClient(
WithServiceAccountToken(token),
WithIntegrationInfo(DefaultIntegrationName, "$"))
require.Error(t, err)
}

func NewTestClientFactory() *ClientFactory {
return &ClientFactory{core: TestCore{}}
}

type TestCore struct{}

func (c TestCore) InitClient(config ClientConfig) (*uint64, error) {
var id uint64
return &id, nil
}

func (c TestCore) Invoke(invokeConfig Invocation) (*string, error) {
response := "secret"
return &response, nil
}

func (c TestCore) ReleaseClient(clientID uint64) {}
191 changes: 0 additions & 191 deletions integration_test.go

This file was deleted.

Loading

0 comments on commit 8464909

Please sign in to comment.