89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
import { Enums } from "@ibcornelsen/database/client";
|
|
import { test, describe, expect } from "bun:test";
|
|
import {faker} from "@faker-js/faker";
|
|
import { client } from "src/trpc";
|
|
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
|
|
import type { AppRouter } from '@ibcornelsen/api';
|
|
import { Buffer } from 'buffer';
|
|
|
|
describe("Bilder hochladen", async () => {
|
|
|
|
// Erstmal lesen wir ein Bild ein, mit dem wir testen können
|
|
const arrayBuffer = await Bun.file("cypress/fixtures/images/daemmung/1.jpeg").arrayBuffer();
|
|
const buffer = Buffer.from(arrayBuffer)
|
|
const base64 = buffer.toString("base64");
|
|
|
|
// Im Browser hätten wir noch einen Mime Type vor dem base64-String
|
|
const base64WithMimeType = "data:image/jpeg;base64," + base64;
|
|
|
|
// Wir müssen uns einloggen, da wir für das Hochladen von Bildern einen Access Token brauchen
|
|
const email = faker.internet.email();
|
|
const passwort = faker.internet.password();
|
|
const signUpResponse = await client.v1.benutzer.erstellen.mutate({
|
|
email,
|
|
passwort,
|
|
name: faker.person.lastName(),
|
|
vorname: faker.person.firstName(),
|
|
})
|
|
|
|
// Jetzt loggen wir uns ein
|
|
const loginResponse = await client.v1.benutzer.getRefreshToken.query({
|
|
email,
|
|
passwort,
|
|
})
|
|
|
|
// Wir brauchen den Access Token
|
|
const accessToken = loginResponse.accessToken;
|
|
|
|
// Der Default Client versucht den Cookie zu lesen, das geht hier natürlich nicht
|
|
const newClient = createTRPCProxyClient<AppRouter>({
|
|
links: [
|
|
httpBatchLink({
|
|
url: 'http://localhost:3001/',
|
|
headers() {
|
|
if (!accessToken) return {};
|
|
|
|
const buffer = Buffer.from(accessToken, 'utf-8');
|
|
const base64 = buffer.toString('base64')
|
|
|
|
return {
|
|
'Authorization': `Bearer ${base64}`,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
});
|
|
|
|
// Wir brauchen noch ein Gebäude, damit wir das Bild hochladen können
|
|
const gebaeude = await newClient.v1.gebaeude.erstellen.mutate({})
|
|
|
|
// Jetzt laden wir das Bild hoch
|
|
const image = await newClient.v1.bilder.upload.mutate({
|
|
base64: base64WithMimeType,
|
|
gebaeude_uid: gebaeude.uid,
|
|
kategorie: Enums.BilderKategorie.Daemmung,
|
|
})
|
|
|
|
test("sollte ohne Fehler hochladen", async () => {
|
|
expect(image).toHaveProperty("uid");
|
|
expect(image.uid).toBeTypeOf("string")
|
|
})
|
|
|
|
test("bild sollte in der Datenbank und abrufbar sein", async () => {
|
|
const bild = await newClient.v1.bilder.getBase64.query({ uid: image.uid });
|
|
|
|
expect(bild).toHaveProperty("base64");
|
|
expect(bild.base64).toBeTypeOf("string");
|
|
expect(bild.base64).toEqual(base64);
|
|
})
|
|
|
|
test("bild sollte entfernbar sein", async () => {
|
|
const response = await newClient.v1.bilder.entfernen.mutate({ uid: image.uid });
|
|
|
|
expect(response).toBeEmpty();
|
|
|
|
// Einmal nachschauen ob es tatächlich gelöscht wurde
|
|
expect(newClient.v1.bilder.getBase64.query({ uid: image.uid })).rejects.toThrow()
|
|
})
|
|
|
|
}); |