add tests for API endpoint: users
This commit is contained in:
172
tests/APIUsers.test.ts
Normal file
172
tests/APIUsers.test.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import { describe, test, expect, vi } from 'vitest';
|
||||
import { GET, POST } from '../src/routes/api/users/+server';
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
import { addUser, getUsers } from '$lib/server/userService';
|
||||
|
||||
vi.mock('$lib/server/userService', () => ({
|
||||
addUser: vi.fn(),
|
||||
getUsers: vi.fn()
|
||||
}));
|
||||
|
||||
vi.mock('bcrypt', () => ({
|
||||
default: {
|
||||
hashSync: vi.fn()
|
||||
}
|
||||
}));
|
||||
|
||||
describe('API-Endpoint: Users', () => {
|
||||
test('Unerlaubter Zugriff', async () => {
|
||||
const event = {
|
||||
locals: {
|
||||
user: null
|
||||
}
|
||||
};
|
||||
|
||||
const response = await GET(event);
|
||||
expect(response.status).toBe(401);
|
||||
|
||||
const errorMessage = { error: 'Unauthorized' };
|
||||
const json = await response.json();
|
||||
expect(json).toEqual(errorMessage);
|
||||
});
|
||||
|
||||
// [INFO] Test auf keine User nicht notwendig, da immer min. ein User vorhanden
|
||||
|
||||
test('Rufe Liste aller User ab', async () => {
|
||||
const fakeLoggedInUser = { id: 'admin', admin: true };
|
||||
const event = {
|
||||
locals: {
|
||||
user: fakeLoggedInUser
|
||||
}
|
||||
};
|
||||
|
||||
const fakeResult = [{ userId: 42, userName: 'admin' }];
|
||||
getUsers.mockReturnValueOnce(fakeResult);
|
||||
|
||||
const response = await GET(event);
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const json = await response.json();
|
||||
expect(json).toEqual(fakeResult);
|
||||
});
|
||||
|
||||
test('Füge Benutzer hinzu: Erfolgreich', async () => {
|
||||
// mock params and third party function calls
|
||||
const fakeLoggedInUser = { id: 'admin', admin: true };
|
||||
const mockLocals = {
|
||||
user: fakeLoggedInUser
|
||||
};
|
||||
|
||||
const fakeUsersAPIURL = `http://localhost:5173/api/users`;
|
||||
const fakeUserID = 42;
|
||||
const fakeUsername = 'admin';
|
||||
const fakeUserPassword = 'pass-123';
|
||||
|
||||
const mockRequest = new Request(fakeUsersAPIURL, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
userName: fakeUsername,
|
||||
userPassword: fakeUserPassword
|
||||
})
|
||||
});
|
||||
|
||||
const mockedHash = 'mocked-hash';
|
||||
bcrypt.hashSync.mockReturnValueOnce(mockedHash);
|
||||
|
||||
const mockedRowInfo = {
|
||||
changes: 1,
|
||||
lastInsertRowid: fakeUserID
|
||||
};
|
||||
addUser.mockReturnValueOnce(mockedRowInfo);
|
||||
|
||||
const response = await POST({
|
||||
request: mockRequest,
|
||||
locals: mockLocals
|
||||
});
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
|
||||
const fakeResult = { userId: fakeUserID, userName: fakeUsername };
|
||||
const json = await response.json();
|
||||
expect(json).toEqual(fakeResult);
|
||||
|
||||
expect(addUser).toHaveBeenCalledWith(fakeUsername, mockedHash);
|
||||
});
|
||||
|
||||
test('Füge Benutzer hinzu: Fehlender Name oder Passwort', async () => {
|
||||
// mock params and third party function calls
|
||||
const fakeLoggedInUser = { id: 'admin', admin: true };
|
||||
const mockLocals = {
|
||||
user: fakeLoggedInUser
|
||||
};
|
||||
|
||||
const fakeUsersAPIURL = `http://localhost:5173/api/users`;
|
||||
const fakeUserID = 42;
|
||||
const fakeUsername = '';
|
||||
const fakeUserPassword = '';
|
||||
|
||||
const mockRequest = new Request(fakeUsersAPIURL, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
userName: fakeUsername,
|
||||
userPassword: fakeUserPassword
|
||||
})
|
||||
});
|
||||
|
||||
const response = await POST({
|
||||
request: mockRequest,
|
||||
locals: mockLocals
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
|
||||
const errorMessage = { error: 'Missing input' };
|
||||
const json = await response.json();
|
||||
expect(json).toEqual(errorMessage);
|
||||
|
||||
expect(addUser).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('Füge Benutzer hinzu: Nicht erfolgreich, keine Datenbankänderung', async () => {
|
||||
// mock params and third party function calls
|
||||
const fakeLoggedInUser = { id: 'admin', admin: true };
|
||||
const mockLocals = {
|
||||
user: fakeLoggedInUser
|
||||
};
|
||||
|
||||
const fakeUsersAPIURL = `http://localhost:5173/api/users`;
|
||||
const fakeUserID = 42;
|
||||
const fakeUsername = 'admin';
|
||||
const fakeUserPassword = 'pass-123';
|
||||
|
||||
const mockRequest = new Request(fakeUsersAPIURL, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
userName: fakeUsername,
|
||||
userPassword: fakeUserPassword
|
||||
})
|
||||
});
|
||||
|
||||
const mockedHash = 'mocked-hash';
|
||||
bcrypt.hashSync.mockReturnValueOnce(mockedHash);
|
||||
|
||||
const mockedRowInfo = {
|
||||
changes: 0,
|
||||
lastInsertRowid: fakeUserID
|
||||
};
|
||||
addUser.mockReturnValueOnce(mockedRowInfo);
|
||||
|
||||
const response = await POST({
|
||||
request: mockRequest,
|
||||
locals: mockLocals
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
|
||||
const body = await response.text();
|
||||
expect(body).toEqual('');
|
||||
|
||||
expect(addUser).toHaveBeenCalledWith(fakeUsername, mockedHash);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user