Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(OLD) refactor: Refactor and reorganize files and exports #39

Closed
wants to merge 22 commits into from

Conversation

raynerljm
Copy link
Contributor

@raynerljm raynerljm commented Apr 26, 2023

(OLD): Some of these changes were implemented in #35 . I will make a new PR specifically for test-related changes subsequently

Problem

General refactoring of files and unit tests

Closes issue #38 https://github.com/orgs/datagovsg/projects/43/views/1?pane=issue&itemId=26650732

Depends on #35

Solution

Refactor the

  • SgidClient class into its own sgidClient.ts file
  • unit tests (split the testing of the functions into their own files)
  • constants into its own constants.ts file
  • generators (e.g. generatePkcePair) into generators.ts file)
  • move decryptPayload function into the utils.ts file and expose it publicly
  • use the index.ts just as a tunnel to export the main exposed functions (i.e. sgidClient.ts, generators.ts, and utils.ts

Tests

All unit tests are still passing after the refactor

image

@raynerljm raynerljm force-pushed the refactor/post-pkce branch from 452cfd1 to 6f27d56 Compare April 27, 2023 02:26
Copy link
Contributor

@kwajiehao kwajiehao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left comments!

redirectUri = this.getFirstRedirectUri(),
codeChallenge,
}: AuthorizationUrlParams): AuthorizationUrlReturn {
if (this.apiVersion !== 2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments on #35 for this

const expected = new URL(MOCK_AUTH_ENDPOINT_V2)
expect(actual.host).toBe(expected.host)
expect(actual.pathname).toBe(expected.pathname)
expect(actual.searchParams.get('client_id')).toBe(MOCK_CLIENT_ID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: currently, there are many repetitive lines of expects across the tests. instead of writing attribute by attribute, what do you think of using either a string or object to compare (especially considering most values are checked against the default values)?

this allows you to construct a base, expected search params object which you can modify as needed to get the expected result

expect(client).toBeDefined()
})

it('should initialise correctly when a PKCS8 private key is provided', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add a test case that checks for whether new lines are stripped from the private key

*/
export const codeVerifierAndChallengePattern = /^[A-Za-z\d\-._~]{43,128}$/
export async function decryptPayload(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also have unit tests for decryptPayload?

})
})

it('should generate authorisation URL correctly when state and codeChallenge is provided', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we don't need state to be mandatory since we are mandating PKCE

expect(actualNumSearchParams).toBe(8)
})

it('should throw when no redirectUri is provided', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a negative test case for when no code challenge is provided

test/callback.spec.ts Show resolved Hide resolved
* Regex pattern that the code verifier and code challenge in the PKCE flow should match according to the PKCE RFC
* https://www.rfc-editor.org/rfc/rfc7636
*/
export const codeVerifierAndChallengePattern = /^[A-Za-z\d\-._~]{43,128}$/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used purely in test code? if so, we should put it together with the test code instead of bundling it together with SDK code. it's good to be deliberate about what we export, as we have to support it forever or make a breaking change to remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm that's a very good point - shifted into the test code in this PR

@raynerljm raynerljm force-pushed the feat/pkce-breaking branch from ef32dbd to d16757e Compare May 5, 2023 02:27
@raynerljm raynerljm marked this pull request as draft May 5, 2023 03:18
@raynerljm raynerljm changed the title refactor: Refactor and reorganize files and exports (OLD) refactor: Refactor and reorganize files and exports May 5, 2023
Base automatically changed from feat/pkce-breaking to develop May 15, 2023 02:55
@raynerljm
Copy link
Contributor Author

raynerljm commented May 26, 2023

Closed because most of these changes were implemented in #35 .

@raynerljm raynerljm closed this May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants