2023-11-21 12:11:38 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
_ "embed"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
)
|
|
|
|
|
2024-09-17 11:34:14 +00:00
|
|
|
func TestQueries_ActiveIntrospectionClientByID(t *testing.T) {
|
2023-11-21 12:11:38 +00:00
|
|
|
pubkeys := database.Map[[]byte]{
|
|
|
|
"key1": {1, 2, 3},
|
|
|
|
"key2": {4, 5, 6},
|
|
|
|
}
|
|
|
|
encPubkeys, err := pubkeys.Value()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expQuery := regexp.QuoteMeta(introspectionClientByIDQuery)
|
|
|
|
type args struct {
|
|
|
|
clientID string
|
|
|
|
getKeys bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
mock sqlExpectation
|
|
|
|
want *IntrospectionClient
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "query error",
|
|
|
|
args: args{
|
|
|
|
clientID: "clientID",
|
|
|
|
getKeys: false,
|
|
|
|
},
|
|
|
|
mock: mockQueryErr(expQuery, sql.ErrConnDone, "instanceID", "clientID", false),
|
|
|
|
wantErr: sql.ErrConnDone,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success, secret",
|
|
|
|
args: args{
|
|
|
|
clientID: "clientID",
|
|
|
|
getKeys: false,
|
|
|
|
},
|
|
|
|
mock: mockQuery(expQuery,
|
2024-04-09 13:15:35 +00:00
|
|
|
[]string{"app_id", "client_id", "client_secret", "app_type", "project_id", "resource_owner", "project_role_assertion", "public_keys"},
|
|
|
|
[]driver.Value{"appID", "clientID", "secret", "oidc", "projectID", "orgID", true, nil},
|
2023-11-21 12:11:38 +00:00
|
|
|
"instanceID", "clientID", false),
|
|
|
|
want: &IntrospectionClient{
|
2024-04-09 13:15:35 +00:00
|
|
|
AppID: "appID",
|
|
|
|
ClientID: "clientID",
|
|
|
|
HashedSecret: "secret",
|
|
|
|
AppType: AppTypeOIDC,
|
|
|
|
ProjectID: "projectID",
|
|
|
|
ResourceOwner: "orgID",
|
|
|
|
ProjectRoleAssertion: true,
|
|
|
|
PublicKeys: nil,
|
2023-11-21 12:11:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success, keys",
|
|
|
|
args: args{
|
|
|
|
clientID: "clientID",
|
|
|
|
getKeys: true,
|
|
|
|
},
|
|
|
|
mock: mockQuery(expQuery,
|
2024-04-09 13:15:35 +00:00
|
|
|
[]string{"app_id", "client_id", "client_secret", "app_type", "project_id", "resource_owner", "project_role_assertion", "public_keys"},
|
|
|
|
[]driver.Value{"appID", "clientID", "", "oidc", "projectID", "orgID", true, encPubkeys},
|
2023-11-21 12:11:38 +00:00
|
|
|
"instanceID", "clientID", true),
|
|
|
|
want: &IntrospectionClient{
|
2024-04-09 13:15:35 +00:00
|
|
|
AppID: "appID",
|
|
|
|
ClientID: "clientID",
|
|
|
|
HashedSecret: "",
|
|
|
|
AppType: AppTypeOIDC,
|
|
|
|
ProjectID: "projectID",
|
|
|
|
ResourceOwner: "orgID",
|
|
|
|
ProjectRoleAssertion: true,
|
|
|
|
PublicKeys: pubkeys,
|
2023-11-21 12:11:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
execMock(t, tt.mock, func(db *sql.DB) {
|
|
|
|
q := &Queries{
|
|
|
|
client: &database.DB{
|
|
|
|
DB: db,
|
|
|
|
Database: &prepareDB{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := authz.NewMockContext("instanceID", "orgID", "userID")
|
2024-09-17 11:34:14 +00:00
|
|
|
got, err := q.ActiveIntrospectionClientByID(ctx, tt.args.clientID, tt.args.getKeys)
|
2023-11-21 12:11:38 +00:00
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|