-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve access service (#4689)
## About the changes This enables us to use names instead of permission ids across all our APIs at the computational cost of searching for the ids in the DB but improving the API user experience ## Open topics We're using methods that are test-only and circumvent our business logic. This makes our test to rely on assumptions that are not always true because these assumptions are not validated frequently. i.e. We are expecting that after removing a permission it's no longer there, but to test this, the permission has to be there before: https://github.com/Unleash/unleash/blob/78273e4ff30d7012aa91f6549115587dc31a6e0b/src/test/e2e/services/access-service.e2e.test.ts#L367-L375 But it seems that's not the case. We'll look into improving this later.
- Loading branch information
1 parent
2843388
commit 2186e2b
Showing
11 changed files
with
270 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import dbInit from '../../test/e2e/helpers/database-init'; | ||
import getLogger from '../../test/fixtures/no-logger'; | ||
import { PermissionRef } from 'lib/services/access-service'; | ||
import { AccessStore } from './access-store'; | ||
|
||
let db; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('access_store_serial', getLogger); | ||
}); | ||
|
||
afterAll(async () => { | ||
if (db) { | ||
await db.destroy(); | ||
} | ||
}); | ||
|
||
// Helper function to make the test cases more readable | ||
const args = (permissions: PermissionRef[], expectations?: PermissionRef[]) => { | ||
if (expectations) { | ||
return [permissions, expectations]; | ||
} else { | ||
return [permissions]; | ||
} | ||
}; | ||
|
||
test('resolvePermissions returns empty list if undefined', async () => { | ||
const access = db.stores.accessStore as AccessStore; | ||
const result = await access.resolvePermissions( | ||
undefined as unknown as PermissionRef[], | ||
); | ||
expect(result).toStrictEqual([]); | ||
}); | ||
|
||
test('resolvePermissions returns empty list if empty list', async () => { | ||
const access = db.stores.accessStore as AccessStore; | ||
const result = await access.resolvePermissions([] as PermissionRef[]); | ||
expect(result).toStrictEqual([]); | ||
}); | ||
|
||
test.each([ | ||
args([{ id: 1 }]), | ||
args([{ id: 4, environment: 'development' }]), | ||
args([{ id: 4, name: 'should keep the id' }]), | ||
args([ | ||
{ id: 1, environment: 'development' }, | ||
{ id: 2, name: 'ignore this name' }, | ||
]), | ||
])( | ||
'resolvePermissions with permission ids (%o) returns the list unmodified', | ||
async (permissions) => { | ||
const access = db.stores.accessStore as AccessStore; | ||
const result = await access.resolvePermissions(permissions); | ||
expect(result).toStrictEqual(permissions); | ||
}, | ||
); | ||
|
||
test.each([ | ||
args( | ||
[{ name: 'CREATE_CONTEXT_FIELD' }], | ||
[{ id: 18, name: 'CREATE_CONTEXT_FIELD' }], | ||
), | ||
args( | ||
[{ name: 'CREATE_FEATURE', environment: 'development' }], | ||
[{ id: 2, name: 'CREATE_FEATURE', environment: 'development' }], | ||
), | ||
args( | ||
[ | ||
{ name: 'CREATE_CONTEXT_FIELD' }, | ||
{ name: 'CREATE_FEATURE', environment: 'development' }, | ||
], | ||
[ | ||
{ id: 18, name: 'CREATE_CONTEXT_FIELD' }, | ||
{ id: 2, name: 'CREATE_FEATURE', environment: 'development' }, | ||
], | ||
), | ||
])( | ||
'resolvePermissions with permission names (%o) will inject the ids', | ||
async (permissions, expected) => { | ||
const access = db.stores.accessStore as AccessStore; | ||
const result = await access.resolvePermissions(permissions); | ||
expect(result).toStrictEqual(expected); | ||
}, | ||
); | ||
|
||
test.each([ | ||
args( | ||
[ | ||
{ name: 'CREATE_CONTEXT_FIELD' }, | ||
{ id: 3 }, | ||
{ name: 'CREATE_FEATURE', environment: 'development' }, | ||
{ id: 15, environment: 'development' }, | ||
{ name: 'UPDATE_FEATURE', environment: 'development' }, | ||
], | ||
[ | ||
{ id: 18, name: 'CREATE_CONTEXT_FIELD' }, | ||
{ id: 3 }, | ||
{ id: 2, name: 'CREATE_FEATURE', environment: 'development' }, | ||
{ id: 15, environment: 'development' }, | ||
{ id: 7, name: 'UPDATE_FEATURE', environment: 'development' }, | ||
], | ||
), | ||
])( | ||
'resolvePermissions mixed ids and names (%o) will inject the ids where they are missing', | ||
async (permissions, expected) => { | ||
const access = db.stores.accessStore as AccessStore; | ||
const result = await access.resolvePermissions(permissions); | ||
expect(result).toStrictEqual(expected); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.