Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions server/account/src/__tests__/mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,3 +1198,21 @@ describe('MongoAccountDB', () => {
})
})
})

describe('MongoAccountDB.hasWorkspacePermission', () => {
function createDb (doc: unknown): MongoAccountDB {
const db = new MongoAccountDB({ collection: jest.fn().mockReturnValue({}) } as unknown as Db)
;(db as any).workspacePermission = { findOne: jest.fn().mockResolvedValue(doc) }
return db
}

it('returns false when no permission document exists', async () => {
const db = createDb(null)
expect(await db.hasWorkspacePermission('acc' as AccountUuid, 'ws' as WorkspaceUuid, 'perm')).toBe(false)
})

it('returns true when a permission document exists', async () => {
const db = createDb({ workspaceUuid: 'ws', accountUuid: 'acc', permission: 'perm' })
expect(await db.hasWorkspacePermission('acc' as AccountUuid, 'ws' as WorkspaceUuid, 'perm')).toBe(true)
})
})
18 changes: 18 additions & 0 deletions server/account/src/__tests__/postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,21 @@ describe('PostgresAccountDB', () => {
})
})
})

describe('PostgresAccountDB.hasWorkspacePermission', () => {
function createDb (rows: any[]): PostgresAccountDB {
const mock: any = Object.assign(jest.fn(), { unsafe: jest.fn().mockResolvedValue(rows) })
mock.begin = jest.fn().mockImplementation((callback: (client: any) => Promise<any>) => callback(mock))
return new PostgresAccountDB(mock as Sql)
}

it('returns false when no permission row exists', async () => {
const db = createDb([])
expect(await db.hasWorkspacePermission('acc' as AccountUuid, 'ws' as WorkspaceUuid, 'perm')).toBe(false)
})

it('returns true when a permission row exists', async () => {
const db = createDb([{ workspace_uuid: 'ws', account_uuid: 'acc', permission: 'perm' }])
expect(await db.hasWorkspacePermission('acc' as AccountUuid, 'ws' as WorkspaceUuid, 'perm')).toBe(true)
})
})
2 changes: 1 addition & 1 deletion server/account/src/collections/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ export class MongoAccountDB implements AccountDB {
accountUuid: accountId,
permission
})
return result !== undefined
return result != null
}

async getWorkspacePermissions (accountId: AccountUuid, permission: string): Promise<WorkspaceUuid[]> {
Expand Down
2 changes: 1 addition & 1 deletion server/account/src/collections/postgres/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ export class PostgresAccountDB implements AccountDB {
accountUuid: accountId,
permission
})
return result !== undefined
return result != null
}

async getWorkspacePermissions (accountId: AccountUuid, permission: string): Promise<WorkspaceUuid[]> {
Expand Down
Loading