gitpod/components/gitpod-db/go/dbtest/oidc_client_config.go
Alex Tugarev 76c61533a6
[OIDC] Enable mark client config as "active" (#17365)
* [gitpod-db] Add `d_b_oidc_client_config.active` field

* [papi] Add OIDCClientConfig.active to proto def

* [gitpod-db] Add OIDCClientConfig.active

* [papi] Add `activate` param to `/oidc/start` endpoint handler

If provided it should mark the OIDC client config as `active` in the DB.

* Fix propagation of state params and add tests.

* fix import of deprecated ioutil

* refactor GetStartParams

* consider `activate` from create request
2023-04-27 21:35:36 +08:00

78 lines
1.7 KiB
Go

// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package dbtest
import (
"context"
"testing"
"time"
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func NewOIDCClientConfig(t *testing.T, record db.OIDCClientConfig) db.OIDCClientConfig {
t.Helper()
cipher, _ := GetTestCipher(t)
encrypted, err := db.EncryptJSON(cipher, db.OIDCSpec{})
require.NoError(t, err)
now := time.Now().UTC().Truncate(time.Millisecond)
result := db.OIDCClientConfig{
ID: uuid.New(),
Issuer: "issuer",
Data: encrypted,
LastModified: now,
}
if record.ID != uuid.Nil {
result.ID = record.ID
}
if record.OrganizationID != uuid.Nil {
result.OrganizationID = record.OrganizationID
}
if record.Issuer != "" {
result.Issuer = record.Issuer
}
if record.Data != nil {
result.Data = record.Data
}
return result
}
func CreateOIDCClientConfigs(t *testing.T, conn *gorm.DB, entries ...db.OIDCClientConfig) []db.OIDCClientConfig {
t.Helper()
var records []db.OIDCClientConfig
var ids []string
for _, entry := range entries {
record := NewOIDCClientConfig(t, entry)
records = append(records, record)
ids = append(ids, record.ID.String())
_, err := db.CreateOIDCClientConfig(context.Background(), conn, record)
require.NoError(t, err)
}
t.Cleanup(func() {
HardDeleteOIDCClientConfigs(t, ids...)
})
return records
}
func HardDeleteOIDCClientConfigs(t *testing.T, ids ...string) {
if len(ids) > 0 {
require.NoError(t, conn.Where(ids).Delete(&db.OIDCClientConfig{}).Error)
}
}