Compare commits
3 Commits
1e96f13d22
...
f18ef07116
| Author | SHA1 | Date | |
|---|---|---|---|
| f18ef07116 | |||
| 821b8a6440 | |||
| e0b490a353 |
163
tests/APIUsers.test.ts
Normal file
163
tests/APIUsers.test.ts
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
import { describe, test, expect, vi, beforeEach } 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
|
||||||
|
|
||||||
|
// Mock eingelogter User bzw. stelle locals.user zur Verfügung
|
||||||
|
const fakeLoggedInUser = { id: 'admin', admin: true };
|
||||||
|
const mockLocals = {
|
||||||
|
user: fakeLoggedInUser
|
||||||
|
};
|
||||||
|
|
||||||
|
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 () => {
|
||||||
|
// Mocke Parameter und Funktionen von Drittparteien
|
||||||
|
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 () => {
|
||||||
|
// Mocke Parameter und Funktionen von Drittparteien
|
||||||
|
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 () => {
|
||||||
|
// Mocke Parameter und Funktionen von Drittparteien
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
43
tests/APIVorgangVorgangVorgangPIN.test.ts
Normal file
43
tests/APIVorgangVorgangVorgangPIN.test.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
|
import { GET } from '../src/routes/api/vorgang/[vorgang]/vorgangPIN/+server';
|
||||||
|
import { db } from '$lib/server/dbService';
|
||||||
|
|
||||||
|
const mockEvent = {
|
||||||
|
params: { vorgang: '123' }
|
||||||
|
};
|
||||||
|
|
||||||
|
vi.mock('$lib/server/dbService', () => ({
|
||||||
|
db: {
|
||||||
|
prepare: vi.fn()
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('API-Endpoint: Vorgang-PIN', () => {
|
||||||
|
test('Vorgang PIN: Erfolgreich', async () => {
|
||||||
|
// only interested in PIN value
|
||||||
|
const mockPIN = 'pin-123';
|
||||||
|
const mockRow = { pin: mockPIN };
|
||||||
|
|
||||||
|
const getMock = vi.fn().mockReturnValue(mockRow);
|
||||||
|
|
||||||
|
db.prepare.mockReturnValue({ get: getMock });
|
||||||
|
const response = await GET(mockEvent);
|
||||||
|
expect(response.status).toBe(200);
|
||||||
|
|
||||||
|
const body = await response.text();
|
||||||
|
expect(body).toEqual(mockPIN);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Vorgang PIN: Nicht erfolgreich', async () => {
|
||||||
|
const mockRow = {};
|
||||||
|
|
||||||
|
const getMock = vi.fn().mockReturnValue(mockRow);
|
||||||
|
|
||||||
|
db.prepare.mockReturnValue({ get: getMock });
|
||||||
|
const response = await GET(mockEvent);
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
|
||||||
|
const body = await response.text();
|
||||||
|
expect(body).toEqual('');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user