generated from nimblehq/git-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
[#25] [#36] [Backend] As a user, I can automatically re-authenticate if my access token expires [Backend] As a user, when I am unauthenticated I can see the Login page [#24] [Backend] As a user, when I am authenticated I can see my profile image at the top of all pages #34
Open
liamstevens111
wants to merge
14
commits into
feature/21-backend-login-email-password
Choose a base branch
from
feature/25-login-page-when-unauthenticated
base: feature/21-backend-login-email-password
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
90c772d
[#36] axios interceptors for appending token or for obtaining refresh
liamstevens111 e3b555e
[#36] Add tests to authAdapter and refactor
liamstevens111 748b9be
Initial private route and context
liamstevens111 a1e44aa
Fix private routes with outlet and OutletContext wip
liamstevens111 06139d8
[#25] Include user profile details in localstorage with token informa…
liamstevens111 f3f0336
[#25] Refactor login tests
liamstevens111 1ca5c97
[#25] Add test for getUser
liamstevens111 949171a
[#25] Add tests for Axios interceptors wip
liamstevens111 8f109f6
[#25] Fix window location href mock
liamstevens111 126b62e
[#25] Add comments for test I cant mock
liamstevens111 b2c209c
[#25] Cleanup naming
liamstevens111 6f1ddc3
[#25] Fix cypress test interceptors and add for prfile for login page
liamstevens111 ec620e6
[#25] Remove comment and disable rule comment
liamstevens111 9f8b49e
[#25] Remove comment and disable rule comment
liamstevens111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 @@ | ||
{ | ||
"data": { | ||
"id": "177", | ||
"type": "user", | ||
"attributes": { | ||
"email": "[email protected]", | ||
"name": "Team Nimble", | ||
"avatar_url": "valid_avatar_url" | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,55 +7,38 @@ describe('User Authentication', () => { | |
}); | ||
}); | ||
|
||
// TODO: The below test fail on CI/CD with the use of Intercept | ||
|
||
context('given valid credentials', () => { | ||
it('redirects to the home page', () => { | ||
cy.intercept('POST', '/oauth/token/', { | ||
context('login with email and password', () => { | ||
it('given correct credentials, redirects to the home page', () => { | ||
cy.intercept('POST', 'api/v1/oauth/token', { | ||
statusCode: 200, | ||
fixture: 'Authentication/valid-credentials.json', | ||
}); | ||
|
||
cy.intercept('GET', '/api/v1/me', { statusCode: 200, fixture: 'Authentication/logged-in-user.json' }); | ||
|
||
cy.visit('/login'); | ||
|
||
cy.get('input[name=email]').type('liam@nimblehq.co'); | ||
cy.get('input[name=password]').type('12345678'); | ||
cy.get('input[name=email]').type('example_email@nimblehq.co'); | ||
cy.get('input[name=password]').type('ValidPassword'); | ||
cy.get('button[type="submit"]').click(); | ||
|
||
cy.location().should((location) => { | ||
expect(location.pathname).to.eq('/'); | ||
}); | ||
}); | ||
}); | ||
|
||
context('given NO credentials entered', () => { | ||
it('shows field validation errors', () => { | ||
cy.visit('/login'); | ||
|
||
cy.get('button[type="submit"]').click(); | ||
|
||
cy.get('.errors').should('be.visible'); | ||
|
||
cy.get('.errors').within(() => { | ||
cy.contains('Email has invalid format'); | ||
cy.contains('Password should be at least'); | ||
}); | ||
cy.findByTestId('app-main-heading').should('be.visible'); | ||
}); | ||
}); | ||
|
||
// TODO: The below test fail on CI/CD with the use of Intercept | ||
|
||
context('given INVALID credentials', () => { | ||
it('shows login error', () => { | ||
cy.intercept('POST', '/oauth/token/', { | ||
it('given INCORRECT credentials, shows login error', () => { | ||
cy.intercept('POST', 'api/v1/oauth/token', { | ||
statusCode: 400, | ||
fixture: 'Authentication/invalid-credentials.json', | ||
}); | ||
|
||
cy.visit('/login'); | ||
|
||
cy.get('input[name=email]').type('[email protected]'); | ||
cy.get('input[name=password]').type('password123'); | ||
cy.get('input[name=email]').type('[email protected]'); | ||
cy.get('input[name=password]').type('InvalidPassword'); | ||
cy.get('button[type="submit"]').click(); | ||
|
||
cy.location().should((location) => { | ||
|
@@ -67,6 +50,21 @@ describe('User Authentication', () => { | |
cy.get('.errors').within(() => { | ||
cy.findByText('Your email or password is incorrect. Please try again.').should('exist'); | ||
}); | ||
|
||
cy.findByTestId('app-main-heading').should('not.exist'); | ||
}); | ||
|
||
it('given NO credentials entered, shows field validation errors', () => { | ||
cy.visit('/login'); | ||
|
||
cy.get('button[type="submit"]').click(); | ||
|
||
cy.get('.errors').should('be.visible'); | ||
|
||
cy.get('.errors').within(() => { | ||
cy.contains('Email has invalid format'); | ||
cy.contains('Password should be at least'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,41 +1,83 @@ | ||
import nock from 'nock'; | ||
|
||
import AuthAdapter from './authAdapter'; | ||
import AuthAdapter, { OauthParams } from './authAdapter'; | ||
|
||
/* eslint-disable camelcase */ | ||
const testCredentials = { | ||
const mockLoginCredentials = { | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
|
||
const commonLoginParams = { | ||
grant_type: 'password', | ||
client_id: process.env.REACT_APP_API_CLIENT_ID, | ||
client_secret: process.env.REACT_APP_API_CLIENT_SECRET, | ||
const commonUserProfileResponse = { | ||
data: { | ||
id: '1', | ||
type: 'user', | ||
attributes: { | ||
email: '[email protected]', | ||
name: 'TestName', | ||
avatar_url: 'https://secure.gravatar.com/avatar/6733d09432e89459dba795de8312ac2d', | ||
}, | ||
}, | ||
}; | ||
/* eslint-enable camelcase */ | ||
|
||
describe('AuthAdapter', () => { | ||
afterAll(() => { | ||
nock.cleanAll(); | ||
nock.restore(); | ||
}); | ||
|
||
describe('login', () => { | ||
describe('loginWithEmailPassword', () => { | ||
test('The login endpoint is called with credientials from the request', async () => { | ||
const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`) | ||
.defaultReplyHeaders({ | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-credentials': 'true', | ||
}) | ||
.post('/oauth/token', { | ||
...testCredentials, | ||
...commonLoginParams, | ||
...mockLoginCredentials, | ||
...OauthParams, | ||
grant_type: 'password', | ||
}) | ||
.reply(200); | ||
|
||
expect(scope.isDone()).toBe(false); | ||
await AuthAdapter.login({ ...testCredentials }); | ||
await AuthAdapter.loginWithEmailPassword({ ...mockLoginCredentials }); | ||
expect(scope.isDone()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('loginWithRefreshToken', () => { | ||
test('The refresh token endpoint is called with refresh token from the request', async () => { | ||
const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`) | ||
.defaultReplyHeaders({ | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-credentials': 'true', | ||
}) | ||
.post('/oauth/token', { | ||
refresh_token: 'refresh_token', | ||
...OauthParams, | ||
grant_type: 'refresh_token', | ||
}) | ||
.reply(200); | ||
|
||
expect(scope.isDone()).toBe(false); | ||
await AuthAdapter.loginWithRefreshToken('refresh_token'); | ||
expect(scope.isDone()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('getUser', () => { | ||
test('The user profile endpoint is called and returns user information', async () => { | ||
const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`) | ||
.defaultReplyHeaders({ | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-credentials': 'true', | ||
}) | ||
.get('/me') | ||
.reply(200, { ...commonUserProfileResponse }); | ||
|
||
expect(scope.isDone()).toBe(false); | ||
expect(await AuthAdapter.getUser()).toEqual({ ...commonUserProfileResponse }); | ||
expect(scope.isDone()).toBe(true); | ||
}); | ||
}); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable camelcase */ | ||
import { MemoryRouter } from 'react-router-dom'; | ||
|
||
import { render, screen, waitForElementToBeRemoved } from '@testing-library/react'; | ||
|
||
import PrivateRoute from '.'; | ||
import { setItem, clearItem } from '../../helpers/localStorage'; | ||
|
||
const mockTokenData = { | ||
access_token: 'test_access_token', | ||
refresh_token: 'test_refresh_token', | ||
token_type: 'Bearer', | ||
expires_in: 7200, | ||
created_at: 1677045997, | ||
}; | ||
|
||
const mockUserProfileData = { | ||
email: '[email protected]', | ||
name: 'TestName', | ||
avatar_url: 'https://secure.gravatar.com/avatar/6733d09432e89459dba795de8312ac2d', | ||
}; | ||
|
||
describe('PrivateRoute', () => { | ||
beforeEach(() => { | ||
clearItem('UserProfile'); | ||
}); | ||
|
||
test('renders a PrivateRoute given authenticated user', async () => { | ||
setItem('UserProfile', { auth: mockTokenData, user: mockUserProfileData }); | ||
|
||
render(<PrivateRoute />, { wrapper: MemoryRouter }); | ||
|
||
expect(localStorage.getItem).toBeCalledWith('UserProfile'); | ||
}); | ||
|
||
test.skip('renders a PrivateRoute', async () => { | ||
// Infinite loop | ||
render(<PrivateRoute />, { wrapper: MemoryRouter }); | ||
|
||
await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading')); | ||
|
||
expect(screen.getByTestId('loading')); | ||
}); | ||
}); |
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,38 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { Navigate, Outlet, useOutletContext } from 'react-router-dom'; | ||
|
||
import { getItem } from 'helpers/localStorage'; | ||
import type { User } from 'types/User'; | ||
|
||
type ContextType = User; | ||
|
||
import { LOGIN_URL } from '../../constants'; | ||
|
||
function PrivateRoute() { | ||
const [user, setUser] = useState<User | null>(null); | ||
const [loading, setLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
const fetchCurrentUser = async () => { | ||
const userProfile = getItem('UserProfile'); | ||
if (userProfile?.user) { | ||
setUser({ ...userProfile.user }); | ||
} | ||
|
||
setLoading(false); | ||
}; | ||
fetchCurrentUser(); | ||
}, []); | ||
|
||
if (loading) { | ||
return <h3>Loading...</h3>; | ||
} | ||
|
||
return user ? <Outlet context={user} /> : <Navigate to={LOGIN_URL} />; | ||
} | ||
|
||
export default PrivateRoute; | ||
|
||
export function useUser() { | ||
return useOutletContext<ContextType>(); | ||
} |
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 +1,3 @@ | ||
export const PASSWORD_MIN_LENGTH = 5; | ||
|
||
export const LOGIN_URL = '/login'; |
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,43 @@ | ||
import { setItem, getItem, clearItem } from './localStorage'; | ||
|
||
/* eslint-disable camelcase */ | ||
describe('setItem', () => { | ||
test('Given a valid UserToken, sets it in LocalStorage', () => { | ||
const testToken = { access_token: 'access_token', refresh_token: 'refesh_token' }; | ||
|
||
setItem('UserProfile', testToken); | ||
|
||
expect(JSON.parse(localStorage.getItem('UserProfile') as string)).toStrictEqual(testToken); | ||
}); | ||
}); | ||
|
||
describe('getItem', () => { | ||
test('Given an existing UserToken in LocalStorage, returns the value', () => { | ||
const testToken = { access_token: 'access_token', refresh_token: 'refesh_token' }; | ||
|
||
localStorage.setItem('UserProfile', JSON.stringify(testToken)); | ||
|
||
expect(getItem('UserProfile')).toStrictEqual(testToken); | ||
|
||
localStorage.clear(); | ||
}); | ||
|
||
test('Given a NON-existing UserToken in LocalStorage, returns null', () => { | ||
expect(getItem('UserProfile')).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('clearItem', () => { | ||
test('Given an existing UserToken in LocalStorage, removes the value', () => { | ||
const testToken = { access_token: 'access_token', refresh_token: 'refesh_token' }; | ||
|
||
localStorage.setItem('UserProfile', JSON.stringify(testToken)); | ||
|
||
expect(JSON.parse(localStorage.getItem('UserProfile') as string)).toStrictEqual(testToken); | ||
|
||
clearItem('UserProfile'); | ||
|
||
expect(JSON.parse(localStorage.getItem('UserProfile') as string)).toBeNull(); | ||
}); | ||
}); | ||
/* eslint-enable camelcase */ |
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,13 @@ | ||
export const getItem = (key: string) => { | ||
const value = localStorage.getItem(key); | ||
|
||
return value && JSON.parse(value); | ||
}; | ||
|
||
export const setItem = (key: string, value: object) => { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
}; | ||
|
||
export const clearItem = (key: string) => { | ||
localStorage.removeItem(key); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rename this function? Because user won't login after running this function but get the new access token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I named it this because the only difference between the login and refresh-token endpoint is we are sending a
refresh_token
instead ofemail/password
, otherwise endpoint and response are identical.I also deemed "login" just to be storing token in localstorage from endpoint response which is what both do.
Do you still think it's better to change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, the user needs to log in with their email/password first before they get a refresh token. The user can't initially log in with a refresh token. So to get a new access token, it sounds like the user refreshes the token, not re-login.
Anyway I don't have a strong opinion about this one, I am fine with both name 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think of that - you're right there's no way to get a refresh_token without logging in first, so I will change the name 👍 @hanam1ni
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As authAdapter changed to all be inside surveyAdapter in the latest survey list PR i have included it here instead:
66a6f8c
@hanam1ni fyi