-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added parsing function for the groupRole string
- Added test coverage for the function. - Fixed logic for displaying the loading indicator.
- Loading branch information
1 parent
063472a
commit a809410
Showing
2 changed files
with
136 additions
and
44 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
107 changes: 83 additions & 24 deletions
107
packages/common-ui/lib/account/__tests__/AccountProvider.test.tsx
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 |
---|---|---|
@@ -1,36 +1,95 @@ | ||
import { DINA_ADMIN, USER } from "../../../types/DinaRoles"; | ||
import { | ||
keycloakGroupNamesToBareGroupNames, | ||
generateKeycloakRolesPerGroup | ||
generateKeycloakRolesPerGroup, | ||
parseGroupRoleDevUser | ||
} from "../AccountProvider"; | ||
|
||
describe("AccountProvider", () => { | ||
// Keycloak test data | ||
const keycloakGroups = [ | ||
"/cnc/" + DINA_ADMIN, | ||
"/cnc/" + USER, | ||
"/aafc/" + USER, | ||
"/othergroup", | ||
"no-leading-slash" | ||
]; | ||
|
||
it("Converts Keycloak's group names to bare group names.", () => { | ||
const bareGroupNames = keycloakGroupNamesToBareGroupNames(keycloakGroups); | ||
|
||
expect(bareGroupNames).toEqual([ | ||
"cnc", | ||
"aafc", | ||
"othergroup", | ||
describe("AccountProvider component", () => { | ||
describe("keycloakGroupNamesToBareGroupNames function", () => { | ||
// Keycloak test data | ||
const keycloakGroups = [ | ||
"/cnc/" + DINA_ADMIN, | ||
"/cnc/" + USER, | ||
"/aafc/" + USER, | ||
"/othergroup", | ||
"no-leading-slash" | ||
]); | ||
]; | ||
|
||
it("Converts Keycloak's group names to bare group names.", () => { | ||
const bareGroupNames = keycloakGroupNamesToBareGroupNames(keycloakGroups); | ||
|
||
expect(bareGroupNames).toEqual([ | ||
"cnc", | ||
"aafc", | ||
"othergroup", | ||
"no-leading-slash" | ||
]); | ||
}); | ||
|
||
it("Converts keycloak group + role name string into a unique key and values.", () => { | ||
const rolesPerGroup = generateKeycloakRolesPerGroup(keycloakGroups); | ||
|
||
expect(rolesPerGroup).toEqual({ | ||
aafc: [USER], | ||
cnc: [DINA_ADMIN, USER] | ||
}); | ||
}); | ||
}); | ||
|
||
it("Converts keycloak group + role name string into a unique key and values.", () => { | ||
const rolesPerGroup = generateKeycloakRolesPerGroup(keycloakGroups); | ||
describe("parseGroupRoleDevUser function", () => { | ||
it("should correctly parse groupRole string and return group names and roles", () => { | ||
const groupRoleString = "/aafc/user, /bicoe/read-only, /aafc/admin"; | ||
const expectedResult = { | ||
groupNames: ["aafc", "bicoe"], | ||
rolesPerGroup: { | ||
aafc: ["user", "admin"], | ||
bicoe: ["read-only"], | ||
}, | ||
}; | ||
|
||
const result = parseGroupRoleDevUser(groupRoleString); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it("should remove duplicates from groupNames array", () => { | ||
const groupRoleString = "/aafc/user, /bicoe/read-only, /aafc/user"; | ||
const expectedResult = { | ||
groupNames: ["aafc", "bicoe"], | ||
rolesPerGroup: { | ||
aafc: ["user"], | ||
bicoe: ["read-only"], | ||
}, | ||
}; | ||
|
||
const result = parseGroupRoleDevUser(groupRoleString); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it("if only one group and role is provided it should correctly parse it", () => { | ||
const groupRoleString = "/aafc/user"; | ||
const expectedResult = { | ||
groupNames: ["aafc"], | ||
rolesPerGroup: { | ||
aafc: ["user"] | ||
}, | ||
}; | ||
|
||
const result = parseGroupRoleDevUser(groupRoleString); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it("should handle an empty groupRole string and return empty group names and roles", () => { | ||
const groupRoleString = ""; | ||
const expectedResult = { | ||
groupNames: [], | ||
rolesPerGroup: {}, | ||
}; | ||
|
||
expect(rolesPerGroup).toEqual({ | ||
aafc: [USER], | ||
cnc: [DINA_ADMIN, USER] | ||
const result1 = parseGroupRoleDevUser(groupRoleString); | ||
const result2 = parseGroupRoleDevUser(null); | ||
expect(result1).toEqual(expectedResult); | ||
expect(result2).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); |