-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* chore: create logout developer endpoint; add test #751 * chore: add logout business logic #751 * Update src/middleware/authenticated.ts Co-authored-by: Ijemma Onwuzulike <[email protected]> * Update src/middleware/authenticated.ts Co-authored-by: Ijemma Onwuzulike <[email protected]> * Update src/middleware/authenticated.ts Co-authored-by: Ijemma Onwuzulike <[email protected]> * Update src/routers/router.ts Co-authored-by: Ijemma Onwuzulike <[email protected]> * chore: sync updates #751 --------- Co-authored-by: Ijemma Onwuzulike <[email protected]>
- Loading branch information
1 parent
7a3f647
commit cc81be4
Showing
9 changed files
with
198 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,55 @@ const developerData = { | |
password: 'password', | ||
}; | ||
|
||
// Generate a unique name using a UUID without hyphens | ||
const generateUniqueName = () => uuid().replace(/-/g, ''); | ||
|
||
const generateUniqueEmail = () => { | ||
// Generate a unique email using a UUID without hyphens and without numbers | ||
const email = `${uuid().replace(/-/g, '')}@testing.com`; | ||
// Remove numbers from the email | ||
return email.replace(/\d/g, ''); | ||
}; | ||
|
||
// Generate a unique password using a UUID without hyphens | ||
const generateUniquePassword = () => uuid().replace(/-/g, ''); | ||
|
||
const newDeveloperData = { | ||
name: `${uuid().replace(/-/g, '')}`, | ||
email: `${uuid().replace(/-/g, '')}@testing.com`, | ||
password: `${uuid()}`, | ||
name: generateUniqueName(), | ||
email: generateUniqueEmail(), | ||
password: generateUniquePassword(), | ||
}; | ||
|
||
const developerOneData = { | ||
name: generateUniqueName(), | ||
email: generateUniqueEmail(), | ||
password: generateUniquePassword(), | ||
}; | ||
|
||
const developerTwoData = { | ||
name: generateUniqueName(), | ||
email: generateUniqueEmail(), | ||
password: generateUniquePassword(), | ||
}; | ||
|
||
const anotherDeveloperData = { | ||
name: generateUniqueName(), | ||
email: generateUniqueEmail(), | ||
password: generateUniquePassword(), | ||
}; | ||
|
||
const malformedDeveloperData = { | ||
email: '[email protected]', | ||
password: 'password', | ||
}; | ||
|
||
export { wordId, exampleId, developerData, newDeveloperData, malformedDeveloperData }; | ||
export { | ||
wordId, | ||
exampleId, | ||
developerData, | ||
newDeveloperData, | ||
developerOneData, | ||
developerTwoData, | ||
anotherDeveloperData, | ||
malformedDeveloperData, | ||
}; |
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,17 +1,64 @@ | ||
import { newDeveloperData } from './__mocks__/documentData'; | ||
import { createDeveloper, loginDeveloper } from './shared/commands'; | ||
import { anotherDeveloperData, developerOneData, newDeveloperData } from './__mocks__/documentData'; | ||
import { createDeveloper, loginDeveloper, logoutDeveloper } from './shared/commands'; | ||
|
||
describe('login', () => { | ||
it('should successfully log a developer in', async () => { | ||
await createDeveloper(newDeveloperData); | ||
const developer = await createDeveloper(newDeveloperData); | ||
expect(developer.status).toEqual(200); | ||
|
||
const data = { | ||
email: newDeveloperData.email, | ||
password: newDeveloperData.password, | ||
}; | ||
|
||
const loginRes = await loginDeveloper(data); | ||
|
||
expect(loginRes.status).toEqual(200); | ||
expect(loginRes.body.developer).toMatchObject(loginRes.body.developer); | ||
}); | ||
|
||
it('should not log a developer in with an incorrect password', async () => { | ||
const developer = await createDeveloper(developerOneData); | ||
expect(developer.status).toEqual(200); | ||
|
||
const data = { | ||
email: developerOneData.email, | ||
password: 'incorrect', | ||
}; | ||
|
||
const loginRes = await loginDeveloper(data); | ||
expect(loginRes.status).toEqual(400); | ||
expect(loginRes.body.error).toEqual(loginRes.body.error); | ||
}); | ||
|
||
it('should not log a developer in with an non-existent email', async () => { | ||
const data = { | ||
email: anotherDeveloperData.email, | ||
password: anotherDeveloperData.password, | ||
}; | ||
|
||
const loginRes = await loginDeveloper(data); | ||
expect(loginRes.status).toEqual(400); | ||
expect(loginRes.body.error).toEqual(loginRes.body.error); | ||
}); | ||
}); | ||
|
||
describe('logout', () => { | ||
it('should successfully log a developer out', async () => { | ||
const developer = await createDeveloper(anotherDeveloperData); | ||
expect(developer.status).toEqual(200); | ||
|
||
const data = { | ||
email: anotherDeveloperData.email, | ||
password: anotherDeveloperData.password, | ||
}; | ||
|
||
const loginRes = await loginDeveloper(data); | ||
expect(loginRes.status).toEqual(200); | ||
|
||
const logoutRes = await logoutDeveloper({ token: loginRes.body.token }); | ||
expect(logoutRes.status).toEqual(200); | ||
expect(logoutRes.body).toMatchObject({ | ||
message: 'Logged out successfully', | ||
}); | ||
}); | ||
}); |
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
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,49 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { JWT_SECRET } from '../siteConstants'; | ||
import { Express } from '../types'; | ||
import { createDbConnection, handleCloseConnection } from '../services/database'; | ||
import { developerSchema } from '../models/Developer'; | ||
|
||
interface DeveloperDataType { | ||
email: string; | ||
iat?: number; | ||
exp?: number; | ||
} | ||
|
||
export const authenticate: Express.MiddleWare = async (req, res, next) => { | ||
let token: string | undefined; | ||
// Check if token is set | ||
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer')) { | ||
[, token] = req.headers.authorization.split(' '); | ||
} | ||
|
||
if (!token) { | ||
return next(new Error('Unauthorized. Please login to continue.')); | ||
} | ||
|
||
let developer: DeveloperDataType; | ||
|
||
// Verify token | ||
try { | ||
developer = jwt.verify(token, JWT_SECRET) as DeveloperDataType; | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
return next(new Error(error.message)); | ||
} | ||
return next(new Error('Invalid token')); | ||
} | ||
|
||
// Check if developer still exists in the database | ||
const connection = createDbConnection(); | ||
const Developer = connection.model('Developer', developerSchema); | ||
const { email } = developer; | ||
const currentUser = await Developer.findOne({ email }); | ||
await handleCloseConnection(connection); | ||
if (!currentUser) { | ||
return next(new Error('This User does not exist')); | ||
} | ||
|
||
// Grant access | ||
req.developer = currentUser; | ||
return next(); | ||
}; |
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
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