Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/organization_test.go
2498 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 db_test
6
7
import (
8
"context"
9
"testing"
10
11
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
12
"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest"
13
"github.com/stretchr/testify/require"
14
)
15
16
func Test_OrganizationsWriteRead(t *testing.T) {
17
conn := dbtest.ConnectForTests(t)
18
19
org := dbtest.CreateOrganizations(t, conn, db.Organization{})[0]
20
21
candidates := []db.Organization{
22
{ID: org.ID},
23
}
24
25
for _, read := range candidates {
26
tx := conn.First(&read)
27
require.NoError(t, tx.Error)
28
require.Equal(t, org.Name, read.Name)
29
require.Equal(t, org.Slug, read.Slug)
30
}
31
}
32
33
func Test_GetOrganizationBySlug(t *testing.T) {
34
conn := dbtest.ConnectForTests(t)
35
36
org := dbtest.CreateOrganizations(t, conn, db.Organization{})[0]
37
38
read, err := db.GetOrganizationBySlug(context.Background(), conn, org.Slug)
39
require.NoError(t, err)
40
require.Equal(t, org.Name, read.Name)
41
require.Equal(t, org.Slug, read.Slug)
42
}
43
44
func Test_GetSingleOrganizationWithActiveSSO(t *testing.T) {
45
46
t.Run("not found when no org exist", func(t *testing.T) {
47
conn := dbtest.ConnectForTests(t)
48
49
_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)
50
require.Error(t, err)
51
require.ErrorIs(t, err, db.ErrorNotFound)
52
})
53
54
t.Run("not found when only orgs without SSO exist", func(t *testing.T) {
55
conn := dbtest.ConnectForTests(t)
56
57
_ = dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})
58
59
_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)
60
require.Error(t, err)
61
require.ErrorIs(t, err, db.ErrorNotFound)
62
})
63
64
t.Run("found single org with active SSO", func(t *testing.T) {
65
conn := dbtest.ConnectForTests(t)
66
67
org := dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})[1]
68
_ = dbtest.CreateOIDCClientConfigs(t, conn, db.OIDCClientConfig{
69
OrganizationID: org.ID,
70
Active: true,
71
})[0]
72
73
found, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)
74
require.NoError(t, err)
75
require.Equal(t, org.ID, found.ID)
76
})
77
78
t.Run("not found single org with inactive SSO", func(t *testing.T) {
79
conn := dbtest.ConnectForTests(t)
80
81
org := dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})[1]
82
_ = dbtest.CreateOIDCClientConfigs(t, conn, db.OIDCClientConfig{
83
OrganizationID: org.ID,
84
Active: false,
85
})[0]
86
87
_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)
88
require.Error(t, err)
89
require.ErrorIs(t, err, db.ErrorNotFound)
90
})
91
92
t.Run("not found if two orgs with active SSO exist", func(t *testing.T) {
93
conn := dbtest.ConnectForTests(t)
94
95
orgs := dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})
96
_ = dbtest.CreateOIDCClientConfigs(t, conn, db.OIDCClientConfig{
97
OrganizationID: orgs[0].ID,
98
Active: true,
99
}, db.OIDCClientConfig{
100
OrganizationID: orgs[1].ID,
101
Active: true,
102
})
103
104
_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)
105
require.Error(t, err)
106
require.ErrorIs(t, err, db.ErrorNotFound)
107
})
108
}
109
110