Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/personal_access_token.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 NewPersonalAccessToken(t *testing.T, record db.PersonalAccessToken) db.PersonalAccessToken {
19
t.Helper()
20
21
now := time.Now().UTC().Round(time.Millisecond)
22
tokenID := uuid.New()
23
24
result := db.PersonalAccessToken{
25
ID: tokenID,
26
UserID: uuid.New(),
27
Hash: "some-secure-hash",
28
Name: "some-name",
29
Scopes: []string{"resource:default", "function:*"},
30
ExpirationTime: now.Add(5 * time.Hour),
31
CreatedAt: now,
32
LastModified: now,
33
}
34
35
if record.ID != uuid.Nil {
36
result.ID = record.ID
37
}
38
39
if record.UserID != uuid.Nil {
40
result.UserID = record.UserID
41
}
42
43
if record.Hash != "" {
44
result.Hash = record.Hash
45
}
46
47
if record.Name != "" {
48
result.Name = record.Name
49
}
50
51
if len(record.Scopes) == 0 {
52
result.Scopes = record.Scopes
53
}
54
55
if !record.ExpirationTime.IsZero() {
56
result.ExpirationTime = record.ExpirationTime
57
}
58
59
if !record.CreatedAt.IsZero() {
60
result.CreatedAt = record.CreatedAt
61
}
62
63
if !record.LastModified.IsZero() {
64
result.LastModified = record.LastModified
65
}
66
67
return result
68
}
69
70
func CreatePersonalAccessTokenRecords(t *testing.T, conn *gorm.DB, entries ...db.PersonalAccessToken) []db.PersonalAccessToken {
71
t.Helper()
72
73
var records []db.PersonalAccessToken
74
var ids []string
75
for _, tokenEntry := range entries {
76
record := NewPersonalAccessToken(t, tokenEntry)
77
records = append(records, record)
78
ids = append(ids, record.ID.String())
79
80
_, err := db.CreatePersonalAccessToken(context.Background(), conn, tokenEntry)
81
require.NoError(t, err)
82
}
83
84
t.Cleanup(func() {
85
if len(ids) > 0 {
86
require.NoError(t, conn.Where(ids).Delete(&db.PersonalAccessToken{}).Error)
87
}
88
})
89
90
return records
91
}
92
93