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

PP-11681 Replace 'base-client' with 'axios-base-client' in 'adminusers.client' #4194

Merged

Conversation

JFSGDS
Copy link
Contributor

@JFSGDS JFSGDS commented Mar 13, 2024

https://payments-platform.atlassian.net/browse/PP-11681

WHAT

  • Change ‘adminusers.client’ methods to use ‘axios-base-client’.
  • Refactor call parameters initialisation.
  • Use ‘async/await’ syntax.
  • Extend ‘instanceof RESTClientError’ test in registration controller to include a test for constructor name. This is in order to identify an error thrown by the new 'axios-base-client'. Possibly needs refactoring.

HOW

All automated tests should pass without the need to be amended.
Existing functionality should remain unaffected.

@@ -53,7 +53,7 @@ async function submitEmailPage (req, res, next) {

res.redirect(paths.register.checkEmail)
} catch (err) {
if (err instanceof RESTClientError) {
if (err instanceof RESTClientError | err.constructor.name === 'RESTClientError') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting - I am wondering why this is needed?
We can chat about this.

Copy link
Contributor

Choose a reason for hiding this comment

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

This has been updated - resolving.

Copy link
Contributor

@iqbalgds iqbalgds left a comment

Choose a reason for hiding this comment

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

Looks great - just the test that needs fixing.

}
)
async function getUserByExternalId (externalId) {
const url = `${baseUrl}${userResource}/${externalId}`
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally this should use encodeURIComponent or a URL builder to avoid injection vulnerabilities or encoding issues if special chars are included.

Copy link
Contributor Author

@JFSGDS JFSGDS Mar 18, 2024

Choose a reason for hiding this comment

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

Good point. Presumably this should be done for for all constructs of url with query string parameters (or at least those though to be provided by user input)?
@iqbalgds and I thought this may be better done as a separate PR. Would you agree?

Copy link
Contributor

Choose a reason for hiding this comment

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

So I tend to do it with everything that's not hard-coded but it's particularly important for user input. In some projects I end up creating a URL model that would be something like:

const url = buildUrl({
  base: baseUrl,
  uriParts: [userResource, externalId],
  query: {
    theKey: 'theValue'
  }
})

For this ticket I think it's reasonable to just use encodeURIComponent on each piece of user input in URLs, but personally if it were me doing it I'd rather use it for all URL parts that aren't hard-coded as I'd find that to be less effort than figuring out which ones are or might be from user input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks Natalie. We'll apply this as a separate PR.

)
async function getUsersByExternalIds (externalIds = []) {
const url = `${baseUrl}${userResource}?ids=${externalIds.join()}`
this.client = new Client(SERVICE_NAME)
Copy link
Contributor

Choose a reason for hiding this comment

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

Using this in this context seems to be unhelpful. I'd suggest using const client = instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having got further through the code I'm wondering whether you'd prefer to use a shared client. I'm not sure from just reading the code whether that's a suitable solution or not but it seems like it might be a good match as the client seems to need configuring before use and the configuration seems to be generic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, this approach seems better. Code changed as suggested.

}
)
async function getValidatedInvite (inviteCode) {
const url = `${baseUrl}/v1/api/invites/${inviteCode}`
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming that inviteCode is from user input (or something the user can manipulate) so it makes my previous comment about encoding more important in this instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Please see earlier comment.

@@ -2,7 +2,8 @@

const lodash = require('lodash')

const baseClient = require('./base-client/base.client')
const { Client } = require('@govuk-pay/pay-js-commons/lib/utils/axios-base-client/axios-base-client')
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it's not related to this PR but I'd suggest avoiding being so specific about the path inside the other project. I'd personally go with:

const { AxiosClient } = require('@govuk-pay/pay-js-commons')

Or if you want it kept separate there are ways to have a shorter path e.g.

const { Client } = require('@govuk-pay/pay-js-commons/axios')

To be really clear: This would need to be changed in pay-js-commons and it's not really part of this PR but it's related to this wider piece of work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. I agree it would be much neater.
I also agree that it's not really part of this PR so I propose creating a new ticket to cover this. It should be applied to other repositories already amended for the new version of pay-js-commons containing the new axios-base-client namely pay-products-ui and pay-frontend.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have created a new ticket for this:
https://payments-platform.atlassian.net/browse/PP-12468

@JFSGDS JFSGDS marked this pull request as ready for review March 22, 2024 10:42
@@ -53,7 +53,7 @@ async function submitEmailPage (req, res, next) {

res.redirect(paths.register.checkEmail)
} catch (err) {
if (err instanceof RESTClientError) {
if (err instanceof RESTClientError | err.constructor.name === 'RESTClientError') {
Copy link
Contributor

Choose a reason for hiding this comment

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

This has been updated - resolving.

…rs.client'

- Change  ‘adminusers.client’ methods to use ‘axios-base-client’.
- Refactor call parameters initialisation.
- Use ‘async/await’ syntax.
@JFSGDS JFSGDS force-pushed the PP-11681_replace_request_with_axios_adminusers_client branch from 09e5f65 to bdf0a32 Compare March 22, 2024 10:51
@JFSGDS JFSGDS merged commit c2c208f into master Mar 22, 2024
12 checks passed
@JFSGDS JFSGDS deleted the PP-11681_replace_request_with_axios_adminusers_client branch March 22, 2024 11:22
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