Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/oidc_client_config.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package dbtest
6
7
import (
8
"context"
9
"testing"
10
"time"
11
12
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
13
"github.com/google/uuid"
14
"github.com/stretchr/testify/require"
15
"gorm.io/gorm"
16
)
17
18
func NewOIDCClientConfig(t *testing.T, record db.OIDCClientConfig) db.OIDCClientConfig {
19
t.Helper()
20
21
cipher, _ := GetTestCipher(t)
22
encrypted, err := db.EncryptJSON(cipher, db.OIDCSpec{
23
ClientID: "oidc-client-id",
24
ClientSecret: "oidc-client-secret",
25
RedirectURL: "https://some-redirect-url.or/not",
26
Scopes: []string{
27
"aint", "never", "gonna", "give", "you", "up",
28
},
29
})
30
require.NoError(t, err)
31
32
now := time.Now().UTC().Truncate(time.Millisecond)
33
result := db.OIDCClientConfig{
34
ID: uuid.New(),
35
OrganizationID: uuid.New(),
36
Issuer: "https://accounts.google.com",
37
Data: encrypted,
38
LastModified: now,
39
Active: false,
40
Verified: db.BoolPointer(false),
41
}
42
43
if record.ID != uuid.Nil {
44
result.ID = record.ID
45
}
46
47
if record.OrganizationID != uuid.Nil {
48
result.OrganizationID = record.OrganizationID
49
}
50
51
if record.Issuer != "" {
52
result.Issuer = record.Issuer
53
}
54
55
if record.Data != nil {
56
result.Data = record.Data
57
}
58
59
if record.Active {
60
result.Active = true
61
}
62
63
if record.Verified != nil && *record.Verified {
64
result.Verified = db.BoolPointer(true)
65
}
66
67
return result
68
}
69
70
func CreateOIDCClientConfigs(t *testing.T, conn *gorm.DB, entries ...db.OIDCClientConfig) []db.OIDCClientConfig {
71
t.Helper()
72
73
var records []db.OIDCClientConfig
74
var ids []string
75
for _, entry := range entries {
76
record := NewOIDCClientConfig(t, entry)
77
records = append(records, record)
78
ids = append(ids, record.ID.String())
79
80
foo, err := db.CreateOIDCClientConfig(context.Background(), conn, record)
81
require.NoError(t, err)
82
require.NotNil(t, foo)
83
}
84
85
t.Cleanup(func() {
86
HardDeleteOIDCClientConfigs(t, ids...)
87
})
88
89
return records
90
}
91
92
func HardDeleteOIDCClientConfigs(t *testing.T, ids ...string) {
93
if len(ids) > 0 {
94
require.NoError(t, conn.Where(ids).Delete(&db.OIDCClientConfig{}).Error)
95
}
96
}
97
98