forked from leobrod44/Smartess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthController.test.js
More file actions
94 lines (74 loc) · 3.11 KB
/
authController.test.js
File metadata and controls
94 lines (74 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const request = require('supertest');
const express = require('express');
const supabase = require('../../config/supabase.js');
const authRoutes = require('../../routes/authRoutes.js');
const app = express();
app.use(express.json());
app.use('/auth', authRoutes);
jest.mock('../../config/supabase', () => ({
auth: {
signInWithPassword: jest.fn(),
signOut: jest.fn(),
},
}));
describe('Auth Controller', () => {
describe('POST /auth/login', () => {
it('should return 200 and a success message with a token on successful login', async () => {
const mockResponse = {
data: {
session: { access_token: 'mocked_token' },
user: { id: 'mocked_user_id' },
},
error: null,
};
supabase.auth.signInWithPassword.mockResolvedValue(mockResponse);
const response = await request(app)
.post('/auth/login')
.send({ email: 'random@email.com', password: 'password' });
expect(response.status).toBe(200);
expect(response.body).toEqual({
message: 'Login successful',
token: 'mocked_token',
user: { id: 'mocked_user_id' },
});
});
it('should return 400 if there is an authentication error', async () => {
const mockError = { error: { message: 'Invalid login credentials' } };
supabase.auth.signInWithPassword.mockResolvedValue(mockError);
const response = await request(app)
.post('/auth/login')
.send({ email: 'wrong@example.com', password: 'wrongpassword' });
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'Invalid login credentials' });
});
it('should return 500 if server error occurs during login', async () => {
supabase.auth.signInWithPassword.mockRejectedValue(new Error('Server error'));
const response = await request(app)
.post('/auth/login')
.send({ email: 'random@email.com', password: 'password' });
expect(response.status).toBe(500);
expect(response.body).toEqual({ error: 'Server error' });
});
});
describe('POST /auth/logout', () => {
it('should return 200 and a success message on successful logout', async () => {
supabase.auth.signOut.mockResolvedValue({ error: null });
const response = await request(app).post('/auth/logout');
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: 'Logout successful' });
});
it('should return 400 if there is an error during logout', async () => {
const mockError = { error: { message: 'Logout error' } };
supabase.auth.signOut.mockResolvedValue(mockError);
const response = await request(app).post('/auth/logout');
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'Logout error' });
});
it('should return 500 if server error occurs during logout', async () => {
supabase.auth.signOut.mockRejectedValue(new Error('Server error'));
const response = await request(app).post('/auth/logout');
expect(response.status).toBe(500);
expect(response.body).toEqual({ error: 'Server error' });
});
});
});