1
1
package idpsync_test
2
2
3
3
import (
4
+ "database/sql"
4
5
"testing"
5
6
6
7
"github.com/golang-jwt/jwt/v4"
@@ -9,9 +10,10 @@ import (
9
10
10
11
"cdr.dev/slog/sloggers/slogtest"
11
12
"github.com/coder/coder/v2/coderd/database"
13
+ "github.com/coder/coder/v2/coderd/database/db2sdk"
14
+ "github.com/coder/coder/v2/coderd/database/dbfake"
12
15
"github.com/coder/coder/v2/coderd/database/dbgen"
13
16
"github.com/coder/coder/v2/coderd/database/dbtestutil"
14
- "github.com/coder/coder/v2/coderd/database/dbtime"
15
17
"github.com/coder/coder/v2/coderd/idpsync"
16
18
"github.com/coder/coder/v2/coderd/runtimeconfig"
17
19
"github.com/coder/coder/v2/testutil"
@@ -46,29 +48,28 @@ func TestParseOrganizationClaims(t *testing.T) {
46
48
func TestSyncOrganizations (t * testing.T ) {
47
49
t .Parallel ()
48
50
51
+ // This test creates some deleted organizations and checks the behavior is
52
+ // correct.
49
53
t .Run ("SyncUserToDeletedOrg" , func (t * testing.T ) {
50
54
ctx := testutil .Context (t , testutil .WaitMedium )
51
55
db , _ := dbtestutil .NewDB (t )
52
56
user := dbgen .User (t , db , database.User {})
53
- extra := dbgen .Organization (t , db , database.Organization {})
54
- dbgen .OrganizationMember (t , db , database.OrganizationMember {
55
- UserID : user .ID ,
56
- OrganizationID : extra .ID ,
57
- })
58
57
59
- // Create a new organization, add in the user as a member, then delete
60
- // the org.
61
- org := dbgen .Organization (t , db , database.Organization {})
62
- dbgen .OrganizationMember (t , db , database.OrganizationMember {
63
- UserID : user .ID ,
64
- OrganizationID : org .ID ,
65
- })
66
-
67
- err := db .UpdateOrganizationDeletedByID (ctx , database.UpdateOrganizationDeletedByIDParams {
68
- UpdatedAt : dbtime .Now (),
69
- ID : org .ID ,
70
- })
71
- require .NoError (t , err )
58
+ // Create orgs for:
59
+ // - stays = User is a member, and stays
60
+ // - leaves = User is a member, and leaves
61
+ // - joins = User is not a member, and joins
62
+ // For deleted orgs, the user **should not** be a member of afterwards.
63
+ // - deletedStays = User is a member of deleted org, and wants to stay
64
+ // - deletedLeaves = User is a member of deleted org, and wants to leave
65
+ // - deletedJoins = User is not a member of deleted org, and wants to join
66
+ stays := dbfake .Organization (t , db ).Members (user ).Do ()
67
+ leaves := dbfake .Organization (t , db ).Members (user ).Do ()
68
+ joins := dbfake .Organization (t , db ).Do ()
69
+
70
+ deletedStays := dbfake .Organization (t , db ).Members (user ).Deleted (true ).Do ()
71
+ deletedLeaves := dbfake .Organization (t , db ).Members (user ).Deleted (true ).Do ()
72
+ deletedJoins := dbfake .Organization (t , db ).Deleted (true ).Do ()
72
73
73
74
// Now sync the user to the deleted organization
74
75
s := idpsync .NewAGPLSync (
@@ -77,27 +78,34 @@ func TestSyncOrganizations(t *testing.T) {
77
78
idpsync.DeploymentSyncSettings {
78
79
OrganizationField : "orgs" ,
79
80
OrganizationMapping : map [string ][]uuid.UUID {
80
- "random" : {org .ID },
81
- "noise" : {uuid .New ()},
81
+ "stay" : {stays .Org .ID , deletedStays .Org .ID },
82
+ "leave" : {leaves .Org .ID , deletedLeaves .Org .ID },
83
+ "join" : {joins .Org .ID , deletedJoins .Org .ID },
82
84
},
83
85
OrganizationAssignDefault : false ,
84
86
},
85
87
)
86
88
87
- err = s .SyncOrganizations (ctx , db , user , idpsync.OrganizationParams {
89
+ err : = s .SyncOrganizations (ctx , db , user , idpsync.OrganizationParams {
88
90
SyncEntitled : true ,
89
91
MergedClaims : map [string ]interface {}{
90
- "orgs" : []string {"random " , "noise " },
92
+ "orgs" : []string {"stay " , "join " },
91
93
},
92
94
})
93
95
require .NoError (t , err )
94
96
95
- mems , err := db .OrganizationMembers (ctx , database.OrganizationMembersParams {
96
- OrganizationID : org .ID ,
97
- UserID : user .ID ,
98
- IncludeSystem : false ,
97
+ orgs , err := db .GetOrganizationsByUserID (ctx , database.GetOrganizationsByUserIDParams {
98
+ UserID : user .ID ,
99
+ Deleted : sql.NullBool {},
99
100
})
100
101
require .NoError (t , err )
101
- require .Len (t , mems , 1 )
102
+ require .Len (t , orgs , 2 )
103
+
104
+ // Verify the user only exists in 2 orgs. The one they stayed, and the one they
105
+ // joined.
106
+ inIDs := db2sdk .List (orgs , func (org database.Organization ) uuid.UUID {
107
+ return org .ID
108
+ })
109
+ require .ElementsMatch (t , []uuid.UUID {stays .Org .ID , joins .Org .ID }, inIDs )
102
110
})
103
111
}
0 commit comments