Path: blob/main/components/gitpod-db/go/organization_test.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package db_test56import (7"context"8"testing"910db "github.com/gitpod-io/gitpod/components/gitpod-db/go"11"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest"12"github.com/stretchr/testify/require"13)1415func Test_OrganizationsWriteRead(t *testing.T) {16conn := dbtest.ConnectForTests(t)1718org := dbtest.CreateOrganizations(t, conn, db.Organization{})[0]1920candidates := []db.Organization{21{ID: org.ID},22}2324for _, read := range candidates {25tx := conn.First(&read)26require.NoError(t, tx.Error)27require.Equal(t, org.Name, read.Name)28require.Equal(t, org.Slug, read.Slug)29}30}3132func Test_GetOrganizationBySlug(t *testing.T) {33conn := dbtest.ConnectForTests(t)3435org := dbtest.CreateOrganizations(t, conn, db.Organization{})[0]3637read, err := db.GetOrganizationBySlug(context.Background(), conn, org.Slug)38require.NoError(t, err)39require.Equal(t, org.Name, read.Name)40require.Equal(t, org.Slug, read.Slug)41}4243func Test_GetSingleOrganizationWithActiveSSO(t *testing.T) {4445t.Run("not found when no org exist", func(t *testing.T) {46conn := dbtest.ConnectForTests(t)4748_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)49require.Error(t, err)50require.ErrorIs(t, err, db.ErrorNotFound)51})5253t.Run("not found when only orgs without SSO exist", func(t *testing.T) {54conn := dbtest.ConnectForTests(t)5556_ = dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})5758_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)59require.Error(t, err)60require.ErrorIs(t, err, db.ErrorNotFound)61})6263t.Run("found single org with active SSO", func(t *testing.T) {64conn := dbtest.ConnectForTests(t)6566org := dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})[1]67_ = dbtest.CreateOIDCClientConfigs(t, conn, db.OIDCClientConfig{68OrganizationID: org.ID,69Active: true,70})[0]7172found, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)73require.NoError(t, err)74require.Equal(t, org.ID, found.ID)75})7677t.Run("not found single org with inactive SSO", func(t *testing.T) {78conn := dbtest.ConnectForTests(t)7980org := dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})[1]81_ = dbtest.CreateOIDCClientConfigs(t, conn, db.OIDCClientConfig{82OrganizationID: org.ID,83Active: false,84})[0]8586_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)87require.Error(t, err)88require.ErrorIs(t, err, db.ErrorNotFound)89})9091t.Run("not found if two orgs with active SSO exist", func(t *testing.T) {92conn := dbtest.ConnectForTests(t)9394orgs := dbtest.CreateOrganizations(t, conn, db.Organization{}, db.Organization{})95_ = dbtest.CreateOIDCClientConfigs(t, conn, db.OIDCClientConfig{96OrganizationID: orgs[0].ID,97Active: true,98}, db.OIDCClientConfig{99OrganizationID: orgs[1].ID,100Active: true,101})102103_, err := db.GetSingleOrganizationWithActiveSSO(context.Background(), conn)104require.Error(t, err)105require.ErrorIs(t, err, db.ErrorNotFound)106})107}108109110