Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/identity.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
"testing"
9
10
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
11
"github.com/google/uuid"
12
"github.com/stretchr/testify/require"
13
"gorm.io/gorm"
14
)
15
16
func NewIdentity(t *testing.T, idnt db.Identity) db.Identity {
17
t.Helper()
18
19
result := db.Identity{
20
AuthProviderID: uuid.NewString(),
21
AuthID: uuid.NewString(),
22
AuthName: "unittest",
23
UserID: uuid.New(),
24
PrimaryEmail: "[email protected]",
25
}
26
27
if idnt.AuthProviderID != "" {
28
result.AuthProviderID = idnt.AuthProviderID
29
}
30
if idnt.AuthID != "" {
31
result.AuthID = idnt.AuthID
32
}
33
if idnt.AuthName != "" {
34
result.AuthName = idnt.AuthName
35
}
36
if idnt.UserID != uuid.Nil {
37
result.UserID = idnt.UserID
38
}
39
if idnt.PrimaryEmail != "" {
40
result.PrimaryEmail = idnt.PrimaryEmail
41
}
42
43
return result
44
}
45
46
func CreateIdentities(t *testing.T, conn *gorm.DB, user ...db.Identity) []db.Identity {
47
t.Helper()
48
49
type tuple struct {
50
AuthProviderID string
51
AuthID string
52
}
53
54
var records []db.Identity
55
var ids []tuple
56
for _, u := range user {
57
record := NewIdentity(t, u)
58
records = append(records, record)
59
ids = append(ids, tuple{
60
AuthProviderID: record.AuthProviderID,
61
AuthID: record.AuthID,
62
})
63
64
}
65
require.NoError(t, conn.CreateInBatches(&records, 1000).Error)
66
67
t.Cleanup(func() {
68
for _, tup := range ids {
69
require.NoError(t, conn.Where("authProviderId = ?", tup.AuthProviderID).Where("authId = ?", tup.AuthID).Delete(&db.User{}).Error)
70
}
71
})
72
73
return records
74
}
75
76