-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2584fce
commit 051ffd5
Showing
17 changed files
with
334 additions
and
219 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import request from 'supertest'; | ||
import { app, server } from '../index'; | ||
import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm'; | ||
import { User } from '../entities/User'; | ||
|
||
beforeAll(async () => { | ||
// Connect to the test database | ||
const connectionOptions = await getConnectionOptions(); | ||
await createConnection({ ...connectionOptions, name: 'testConnection' }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await getConnection('testConnection').close(); | ||
server.close(); | ||
}); | ||
|
||
describe('POST /user/logout', () => { | ||
it('should logout a user', async () => { | ||
// sign up a user | ||
const registerUser = { | ||
firstName: 'Ndevu', | ||
lastName: 'Elisa', | ||
email: 'ndevukumurindi@gmail.com', | ||
gender: 'male', | ||
phoneNumber: '078907987443', | ||
photoUrl: 'https://example.com/images/photo.jpg', | ||
userType: 'vender', | ||
verified: true, | ||
status: 'active', | ||
password: process.env.TEST_USER_LOGIN_PASS, | ||
}; | ||
|
||
await request(app).post('/user/register').send(registerUser); | ||
|
||
const loginUser = { | ||
email: registerUser.email, | ||
password: process.env.TEST_USER_LOGIN_PASS, | ||
}; | ||
|
||
const userRepository = getRepository(User); | ||
const user = await userRepository.findOne({ where: { email: registerUser.email } }); | ||
if (user) { | ||
const verifyRes = await request(app).get(`/user/verify/${user.id}`); | ||
|
||
if (!verifyRes) throw new Error(`Test User verification failed for ${user.email}`); | ||
|
||
const loginResponse = await request(app).post('/user/login').send(loginUser); | ||
const setCookie = loginResponse.headers['set-cookie']; | ||
|
||
if (!setCookie) { | ||
throw new Error('No cookies set in login response'); | ||
} | ||
|
||
const resp = await request(app).post('/user/logout').set('Cookie', setCookie); | ||
expect(resp.status).toBe(200); | ||
expect(resp.body).toEqual({ Message: 'Logged out successfully' }); | ||
|
||
// Clean up: delete the test user | ||
await userRepository.remove(user); | ||
} | ||
}); | ||
|
||
it('should not logout a user who is not logged in or with no token', async () => { | ||
const fakeEmail = 'ndevukkkk@gmail.com'; | ||
const loginUser = { | ||
email: fakeEmail, | ||
password: process.env.TEST_USER_LOGIN_PASS, | ||
}; | ||
const token = ''; | ||
const res = await request(app).post('/user/logout').send(loginUser).set('Cookie', token); | ||
expect(res.status).toBe(400); | ||
expect(res.body).toEqual({ Message: 'Access denied. You must be logged in' }); | ||
}); | ||
}); |
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
Oops, something went wrong.