Files
upstream-corteza/server/store/tests/credentials_test.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

149 lines
4.1 KiB
Go

package tests
import (
"context"
"strings"
"testing"
"time"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/rand"
"github.com/cortezaproject/corteza/server/store"
// "github.com/cortezaproject/corteza/server/pkg/rh"
"github.com/cortezaproject/corteza/server/system/types"
_ "github.com/joho/godotenv/autoload"
"github.com/stretchr/testify/require"
)
func testCredentials(t *testing.T, s store.Credentials) {
var (
ctx = context.Background()
makeNew = func(nn ...string) *types.Credential {
name := strings.Join(nn, "")
return &types.Credential{
ID: id.Next(),
OwnerID: id.Next(),
Kind: "test-kind" + name,
Credentials: name,
Label: "CredentialsCRUD" + name,
CreatedAt: time.Now(),
}
}
truncAndCreate = func(t *testing.T) (*require.Assertions, *types.Credential) {
req := require.New(t)
req.NoError(s.TruncateCredentials(ctx))
credentials := makeNew()
req.NoError(s.CreateCredential(ctx, credentials))
return req, credentials
}
truncAndFill = func(t *testing.T, l int) (*require.Assertions, types.CredentialSet) {
req := require.New(t)
req.NoError(s.TruncateCredentials(ctx))
set := make([]*types.Credential, l)
for i := 0; i < l; i++ {
set[i] = makeNew(string(rand.Bytes(10)))
}
req.NoError(s.CreateCredential(ctx, set...))
return req, set
}
)
t.Run("create", func(t *testing.T) {
req := require.New(t)
req.NoError(s.CreateCredential(ctx, makeNew()))
})
t.Run("lookup by ID", func(t *testing.T) {
req, crd := truncAndCreate(t)
fetched, err := s.LookupCredentialByID(ctx, crd.ID)
req.NoError(err)
req.Equal(crd.ID, fetched.ID)
req.NotNil(fetched.CreatedAt)
req.Nil(fetched.UpdatedAt)
req.Nil(fetched.DeletedAt)
})
t.Run("update", func(t *testing.T) {
req, crd := truncAndCreate(t)
crd.Credentials = "new-credentials"
req.NoError(s.UpdateCredential(ctx, crd))
fetched, err := s.LookupCredentialByID(ctx, crd.ID)
req.NoError(err)
req.Equal("new-credentials", fetched.Credentials)
})
t.Run("update", func(t *testing.T) {
req, crd := truncAndCreate(t)
crd.Credentials = "new-credentials"
req.NoError(s.UpdateCredential(ctx, crd))
fetched, err := s.LookupCredentialByID(ctx, crd.ID)
req.NoError(err)
req.Equal("new-credentials", fetched.Credentials)
})
t.Run("delete", func(t *testing.T) {
t.Run("by credentials", func(t *testing.T) {
req, crd := truncAndCreate(t)
req.NoError(s.DeleteCredential(ctx, crd))
set, _, err := s.SearchCredentials(ctx, types.CredentialFilter{OwnerID: crd.OwnerID})
req.NoError(err)
req.Len(set, 0)
})
t.Run("by ID", func(t *testing.T) {
req, crd := truncAndCreate(t)
req.NoError(s.DeleteCredentialByID(ctx, crd.ID))
set, _, err := s.SearchCredentials(ctx, types.CredentialFilter{OwnerID: crd.OwnerID})
req.NoError(err)
req.Len(set, 0)
})
})
t.Run("search", func(t *testing.T) {
t.Run("by owner", func(t *testing.T) {
req, prefill := truncAndFill(t, 5)
set, _, err := s.SearchCredentials(ctx, types.CredentialFilter{OwnerID: prefill[0].OwnerID})
req.NoError(err)
req.Len(set, 1)
})
t.Run("by kind", func(t *testing.T) {
req, prefill := truncAndFill(t, 5)
set, _, err := s.SearchCredentials(ctx, types.CredentialFilter{Kind: prefill[0].Kind})
req.NoError(err)
req.Len(set, 1)
})
// Not sure why this doesnt work
// t.Run("by state", func(t *testing.T) {
// t.Run("deleted", func(t *testing.T) {
// req, prefill := truncAndFill(t, 5)
// time := time.Now()
// prefill[0].DeletedAt = &time
// req.NoError(s.DeleteCredentialsByID(ctx, prefill[0].ID))
// set, _, err := s.SearchCredentials(ctx, types.CredentialFilter{Deleted: filter.StateExcluded})
// req.NoError(err)
// req.Len(set, 4)
// set, _, err = s.SearchCredentials(ctx, types.CredentialFilter{Deleted: filter.StateInclusive})
// req.NoError(err)
// req.Len(set, 5)
// set, _, err = s.SearchCredentials(ctx, types.CredentialFilter{Deleted: filter.StateExclusive})
// req.NoError(err)
// req.Len(set, 1)
// })
// })
})
}