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

212 password flow #281

Merged
merged 14 commits into from
Aug 21, 2017
Merged

212 password flow #281

merged 14 commits into from
Aug 21, 2017

Conversation

hisabimbola
Copy link
Contributor

@hisabimbola hisabimbola commented Aug 18, 2017

Summary

Fixes #212

Description

Todo

  • Tests
    • Unit
    • Integration
    • Acceptance
  • Documentation

Abimbola Idowu added 5 commits August 14, 2017 18:02
affects: @commercetools/sdk-middleware-auth
affects: @commercetools/sdk-middleware-auth

ISSUES CLOSED: #212
affects: @commercetools/sdk-middleware-auth
affects: @commercetools/sdk-middleware-auth
affects: @commercetools/sdk-middleware-auth
@codecov
Copy link

codecov bot commented Aug 18, 2017

Codecov Report

Merging #281 into master will increase coverage by 0.17%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #281      +/-   ##
==========================================
+ Coverage   97.74%   97.92%   +0.17%     
==========================================
  Files          70       72       +2     
  Lines        1555     1590      +35     
  Branches      341      350       +9     
==========================================
+ Hits         1520     1557      +37     
+ Misses         29       27       -2     
  Partials        6        6
Impacted Files Coverage Δ
...sdk-middleware-auth/src/client-credentials-flow.js 100% <100%> (+2.77%) ⬆️
packages/sdk-middleware-auth/src/utils.js 100% <100%> (ø)
packages/sdk-middleware-auth/src/build-requests.js 100% <100%> (ø) ⬆️
packages/sdk-middleware-auth/src/base-auth-flow.js 100% <100%> (ø)
packages/sdk-middleware-auth/src/password-flow.js 100% <100%> (+100%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1020430...3a42900. Read the comment docs.

},
scopes: allScopes,
...options,
}
}

describe('buildRequestForPasswordFlow', () => {
const body = `grant_type=password&scope=${allScopes.join(' ')}\
Copy link
Contributor

Choose a reason for hiding this comment

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

oneLineTrim?

Copy link
Contributor

@adnasa adnasa left a comment

Choose a reason for hiding this comment

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

you should switch the values around in your assertions

expect(request).toEqual(actualParams.request)
expect(response).toEqual(actualParams.response)
expect(response).toEqual(actualParams.response)
expect([]).toEqual(actualParams.pendingTasks)
Copy link
Contributor

Choose a reason for hiding this comment

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

turn this around.

expect(pendingTasks).toEqual([])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, my bad...wondering what I was thinking... 😃

expect(response).toEqual(actualParams.response)
expect([]).toEqual(actualParams.pendingTasks)
expect(
'https://auth.commercetools.co/oauth/foo/token/customers/token',
Copy link
Contributor

Choose a reason for hiding this comment

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

Can use oneLineTrim

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need for oneLineTrim here. Could not see how it will make this better.

expect(request).toEqual(actualParams.request)
expect(response).toEqual(actualParams.response)
expect(response).toEqual(actualParams.response)
expect([]).toEqual(actualParams.pendingTasks)
Copy link
Contributor

Choose a reason for hiding this comment

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

same here... you should turn it around

expect(
'https://auth.commercetools.co/oauth/token',
).toBe(actualParams.url)
expect('MTIzOnNlY3JldA==').toBe(actualParams.basicAuth)
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

const middlewareOptions = createTestMiddlewareOptions()
let requestCount = 0
nock(middlewareOptions.host)
.persist() // <-- use the same interceptor for all requests
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to comment this

Abimbola Idowu added 2 commits August 18, 2017 14:02
affects: @commercetools/sdk-middleware-auth
affects: @commercetools/sdk-middleware-auth
token: string;
expirationTime: number;
}
type AuthMiddlewareBaseOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it not be better to add the type-declarations in the types folder and import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's only used here, hence the reason but yeah can be refactored to the types folder

user,
} = options.credentials
const pKey = options.projectKey
if (!(clientId && clientSecret && user))
Copy link
Contributor

Choose a reason for hiding this comment

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

For better handling, this could be checked one after the other

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you explain what you mean by "better handling"?

Copy link
Contributor

Choose a reason for hiding this comment

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

"Better usability"
If I have the clientIdand clientSecret but no user, it still throws with the same error:
Missing required credentials (clientId, clientSecret, user)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, all is at the same level else we can as well begin to validate each field, and that won't make too much sense.

That's why I am validating clientId and clientSecret and user on a level, and user.username and user.password on another level.

That's my thinking

}
const next = (actualParams) => {
expect(actualParams.request).toEqual(actualParams.request)
expect(actualParams.response).toEqual(actualParams.response)
Copy link
Contributor

Choose a reason for hiding this comment

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

?! 👆🏽

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Damn!!!

)
expect('MTIzOnNlY3JldA==').toBe(actualParams.basicAuth)
Copy link
Contributor

Choose a reason for hiding this comment

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

switch this please...

'This function should never be called, the response was rejected',
const next = (actualParams) => {
expect(actualParams.request).toEqual(actualParams.request)
expect(actualParams.response).toEqual(actualParams.response)
Copy link
Contributor

Choose a reason for hiding this comment

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

🐴

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😮 🙀

clientSecret,
user,
} = options.credentials
const pKey = options.projectKey
Copy link
Contributor

Choose a reason for hiding this comment

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

pKey -> projectKey

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shorter var names to avoid eslint error and unnecessary multiline

I think it's a good trade off 🤷‍♂️

Abimbola Idowu added 2 commits August 21, 2017 13:04
affects: @commercetools/sdk-middleware-auth
affects: @commercetools/sdk-middleware-auth
@hisabimbola
Copy link
Contributor Author

Updated PR @adnasa @wizzy25.

Please check it again

@hisabimbola hisabimbola mentioned this pull request Aug 21, 2017
4 tasks
// If so, then go directly to the next middleware.
if (
(request.headers && request.headers['authorization']) ||
(request.headers && request.headers['Authorization'])
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you covering both?
isn't it better to go for one of them and allow the other to fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's can be an issue that occurred from the http module used when fetching OAuth token from the API

const request = createTestRequest()
const next = (req /* , res */) => {
expect(req).toEqual({
...request,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you setting this up via createTestRequest?

const request = createTestRequest({
     headers: {
          Authorizaton: 'Bearer xxx'
    }
})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to make the code more DRY

Copy link
Contributor

Choose a reason for hiding this comment

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

use toMatchObject

const oauthUri = options.oauthUri || `/oauth/${pKey}/customers/token`
const url = options.host.replace(/\/$/, '') + oauthUri
// eslint-disable-next-line max-len
const body = `grant_type=password&username=${username}&password=${password}${scopeStr}`
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought we were moving towards using oneLineTrim,
one good thing about it is that it increases readability which this line doesn't offer. please change that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this is in the main code (that is being used in the browser also), I don't think it make sense to add a new module to the build just to trim new line.

Not a valid trade off from my point of view.

const request = createTestRequest()
const next = (req /* , res */) => {
expect(req).toEqual({
...request,
Copy link
Contributor

Choose a reason for hiding this comment

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

use toMatchObject

affects: @commercetools/sdk-middleware-auth
@hisabimbola
Copy link
Contributor Author

Refactored here 3a42900, thanks @adnasa for the tip, the assertion looks better now....

@hisabimbola hisabimbola merged commit dc991d2 into master Aug 21, 2017
@hisabimbola hisabimbola deleted the 212_password_flow branch August 21, 2017 16:13
hisabimbola pushed a commit that referenced this pull request Aug 22, 2017
affects: @commercetools/sdk-middleware-auth

ISSUES CLOSED: #212
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