Skip to content

Commit e6ce0f5

Browse files
committed
fix dbfake
1 parent 0cb0d18 commit e6ce0f5

File tree

3 files changed

+45
-71
lines changed

3 files changed

+45
-71
lines changed

coderd/database/dbfake/dbfake.go

Lines changed: 37 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -651,48 +651,6 @@ func (q *FakeQuerier) isEveryoneGroup(id uuid.UUID) bool {
651651
return false
652652
}
653653

654-
func (q *FakeQuerier) GetWorkspaceAgentAndOwnerByAuthToken(ctx context.Context, authToken uuid.UUID) (database.GetWorkspaceAgentAndOwnerByAuthTokenRow, error) {
655-
q.mutex.RLock()
656-
defer q.mutex.RUnlock()
657-
var resp database.GetWorkspaceAgentAndOwnerByAuthTokenRow
658-
var found bool
659-
for _, agt := range q.workspaceAgents {
660-
if agt.AuthToken == authToken {
661-
resp.WorkspaceAgent = agt
662-
found = true
663-
break
664-
}
665-
}
666-
if !found {
667-
return resp, sql.ErrNoRows
668-
}
669-
670-
// get the related workspace and user
671-
for _, res := range q.workspaceResources {
672-
if resp.WorkspaceAgent.ResourceID != res.ID {
673-
continue
674-
}
675-
for _, build := range q.workspaceBuilds {
676-
if build.JobID != res.JobID {
677-
continue
678-
}
679-
for _, ws := range q.workspaces {
680-
if build.WorkspaceID != ws.ID {
681-
continue
682-
}
683-
resp.WorkspaceID = ws.ID
684-
if usr, err := q.getUserByIDNoLock(ws.OwnerID); err == nil {
685-
resp.OwnerID = usr.ID
686-
resp.OwnerRoles = usr.RBACRoles
687-
resp.OwnerName = usr.Username
688-
return resp, nil
689-
}
690-
}
691-
}
692-
}
693-
return database.GetWorkspaceAgentAndOwnerByAuthTokenRow{}, sql.ErrNoRows
694-
}
695-
696654
func (*FakeQuerier) AcquireLock(_ context.Context, _ int64) error {
697655
return xerrors.New("AcquireLock must only be called within a transaction")
698656
}
@@ -2837,36 +2795,48 @@ func (q *FakeQuerier) GetWorkspaceAgentAndOwnerByAuthToken(_ context.Context, au
28372795
q.mutex.RLock()
28382796
defer q.mutex.RUnlock()
28392797
var resp database.GetWorkspaceAgentAndOwnerByAuthTokenRow
2840-
var found bool
2798+
AgentLoop:
28412799
for _, agt := range q.workspaceAgents {
2842-
if agt.AuthToken == authToken {
2843-
resp.WorkspaceAgent = agt
2844-
found = true
2845-
break
2846-
}
2847-
}
2848-
if !found {
2849-
return resp, sql.ErrNoRows
2850-
}
2851-
2852-
// get the related workspace and user
2853-
for _, res := range q.workspaceResources {
2854-
if resp.WorkspaceAgent.ResourceID != res.ID {
2855-
continue
2856-
}
2857-
for _, build := range q.workspaceBuilds {
2858-
if build.JobID != res.JobID { // <-- jobID does not match up
2859-
continue
2800+
if agt.AuthToken != authToken {
2801+
continue AgentLoop
2802+
}
2803+
// get the related workspace and user
2804+
ResourceLoop:
2805+
for _, res := range q.workspaceResources {
2806+
if agt.ResourceID != res.ID {
2807+
continue ResourceLoop
28602808
}
2861-
for _, ws := range q.workspaces {
2862-
if build.WorkspaceID != ws.ID {
2863-
continue
2809+
BuildLoop:
2810+
for _, build := range q.workspaceBuilds {
2811+
if build.JobID != res.JobID {
2812+
continue BuildLoop
28642813
}
2865-
resp.WorkspaceID = ws.ID
2866-
if usr, err := q.getUserByIDNoLock(ws.OwnerID); err == nil {
2814+
WorkspaceLoop:
2815+
for _, ws := range q.workspaces {
2816+
if build.WorkspaceID != ws.ID {
2817+
continue WorkspaceLoop
2818+
}
2819+
resp.WorkspaceID = ws.ID
2820+
usr, err := q.getUserByIDNoLock(ws.OwnerID)
2821+
if err != nil {
2822+
return database.GetWorkspaceAgentAndOwnerByAuthTokenRow{}, sql.ErrNoRows
2823+
}
28672824
resp.OwnerID = usr.ID
2868-
resp.OwnerRoles = usr.RBACRoles
2825+
resp.OwnerRoles = append(usr.RBACRoles, "member")
2826+
// We also need to get org roles for the user
28692827
resp.OwnerName = usr.Username
2828+
resp.WorkspaceAgent = agt
2829+
for _, mem := range q.organizationMembers {
2830+
if mem.UserID == usr.ID {
2831+
resp.OwnerRoles = append(resp.OwnerRoles, fmt.Sprintf("organization-member:%s", mem.OrganizationID.String()))
2832+
}
2833+
}
2834+
// And group memberships
2835+
for _, groupMem := range q.groupMembers {
2836+
if groupMem.UserID == usr.ID {
2837+
resp.OwnerGroups = append(resp.OwnerGroups, groupMem.GroupID.String())
2838+
}
2839+
}
28702840
return resp, nil
28712841
}
28722842
}

coderd/database/queries.sql.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,11 @@ FROM users
229229
ON
230230
group_members.user_id = users.id
231231
WHERE
232+
-- TODO: we can add more conditions here, such as:
233+
-- 1) The user must be active
234+
-- 2) The user must not be deleted
235+
-- 3) The workspace must be running
232236
workspace_agents.auth_token = @auth_token
233-
AND
234-
users.status = 'active' -- workspaces that belong to inactive users should not be
235237
GROUP BY
236238
workspace_agents.id, workspaces.id, users.id, organization_members.organization_id
237239
LIMIT 1;

0 commit comments

Comments
 (0)