Files
ApiCore/ApiCore-Backend/test/weather.e2e-spec.ts
T
Nick SzaunigandClaude Opus 5.5 291ec1d7ff ci: Backend-e2e als eigener Workflow mit Postgres-Service
backend-e2e.yml läuft bei Änderungen unter ApiCore-Backend/ gegen einen
Wegwerf-Postgres 18.6 (per Digest), mit frisch erzeugten JWT-Secrets
und SKIP_EXTERNAL=1 für den Wetter-Test gegen die echte API. Der
Herdentest prüft globalen Zustand, den parallele Suites verändern; er
wiederholt die herdenabhängigen Fälle bis zu dreimal aus einem
zurückgesetzten Zustand. Lokal 3 volle parallele Läufe grün
(103 Suites, 1073 Tests).

Refs #32

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-16 14:39:08 +02:00

130 lines
4.1 KiB
TypeScript

import { INestApplication } from '@nestjs/common';
import * as bcrypt from 'bcryptjs';
import request from 'supertest';
import { App } from 'supertest/types';
import { PrismaService } from '../src/prisma/prisma.service';
import { createTestApp } from './utils/create-test-app';
import { login } from './utils/api';
interface DailyWeatherBody {
date: string;
tempMaxC: number | null;
tempMinC: number | null;
precipitationMm: number | null;
}
interface WeatherBody {
daily: DailyWeatherBody[];
}
interface TrachtprognoseBody {
rating: string;
season: string;
reasoning: string[];
historicalAverageYieldKg: number | null;
}
// These two cases call the real Open-Meteo API. CI sets SKIP_EXTERNAL=1 so the
// pipeline does not depend on the internet; locally they run as before.
const itExternal = process.env.SKIP_EXTERNAL === '1' ? it.skip : it;
describe('Weather (e2e)', () => {
let app: INestApplication<App>;
let prisma: PrismaService;
const beekeeperEmail = `weather-e2e-beekeeper-${Date.now()}@example.com`;
const password = 'SuperSecret123!';
let beekeeperId: string;
let beekeeperToken: string;
let apiaryWithCoordsId: string;
let apiaryWithoutCoordsId: string;
beforeAll(async () => {
app = await createTestApp();
prisma = app.get(PrismaService);
const passwordHash = await bcrypt.hash(password, 12);
const beekeeper = await prisma.user.create({
data: {
email: beekeeperEmail,
passwordHash,
name: 'Weather E2E Beekeeper',
role: { connect: { key: 'BEEKEEPER' } },
},
});
beekeeperId = beekeeper.id;
beekeeperToken = (await login(app, beekeeperEmail, password)).accessToken;
const withCoords = await prisma.apiary.create({
data: {
name: `Weather E2E Stand mit Koordinaten ${Date.now()}`,
latitude: 52.52,
longitude: 13.405,
},
});
apiaryWithCoordsId = withCoords.id;
const withoutCoords = await prisma.apiary.create({
data: { name: `Weather E2E Stand ohne Koordinaten ${Date.now()}` },
});
apiaryWithoutCoordsId = withoutCoords.id;
});
afterAll(async () => {
await prisma.apiary.deleteMany({
where: { id: { in: [apiaryWithCoordsId, apiaryWithoutCoordsId] } },
});
await prisma.refreshToken.deleteMany({ where: { userId: beekeeperId } });
await prisma.auditLog.deleteMany({ where: { userId: beekeeperId } });
await prisma.user.delete({ where: { id: beekeeperId } });
await app.close();
});
itExternal(
'returns real daily weather for an apiary with coordinates',
async () => {
const response = await request(app.getHttpServer())
.get(`/api/v1/apiaries/${apiaryWithCoordsId}/weather`)
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(200);
const body = response.body as WeatherBody;
expect(Array.isArray(body.daily)).toBe(true);
expect(body.daily.length).toBeGreaterThan(15);
const today = body.daily[0];
expect(today.date).toEqual(expect.stringMatching(/^\d{4}-\d{2}-\d{2}$/));
},
15000,
);
itExternal(
'returns a Trachtprognose with a valid rating for an apiary with coordinates',
async () => {
const response = await request(app.getHttpServer())
.get(`/api/v1/apiaries/${apiaryWithCoordsId}/trachtprognose`)
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(200);
const body = response.body as TrachtprognoseBody;
expect(['GUT', 'MAESSIG', 'SCHWACH', 'KEINE_TRACHT']).toContain(
body.rating,
);
expect(body.season).toEqual(expect.any(String));
expect(body.reasoning.length).toBeGreaterThan(0);
},
15000,
);
it('rejects weather requests for an apiary without coordinates', () => {
return request(app.getHttpServer())
.get(`/api/v1/apiaries/${apiaryWithoutCoordsId}/weather`)
.set('Authorization', `Bearer ${beekeeperToken}`)
.expect(400);
});
it('rejects unauthenticated weather requests', () => {
return request(app.getHttpServer())
.get(`/api/v1/apiaries/${apiaryWithCoordsId}/weather`)
.expect(401);
});
});