full upgrade (#31)

*  chore: update CI workflow to include testing and multi-arch build (#29)

*  chore: remove unused dependencies from package-lock.json and updated to a better local dev environment (#30)

*  test: add unit tests

*  test: add unit tests for whole project

*  feat: add whatwg-fetch for improved fetch support

*  chore: update Node.js version to 22 in workflow

*  refactor: update types and improve email handling tests

*  refactor: remove unused imports

*  fix: normalize image name to lowercase in workflows

*  fix: ensure Docker image names are consistently lowercase

*  chore: update

*  chore: update base URL to use secret variable

*  chore: update to login to ghcr

*  fix: add missing 'fi' to close if statement in workflow
This commit is contained in:
Denshooter
2025-02-16 16:36:21 +01:00
committed by GitHub
parent b4616234cf
commit 180b9aa9f8
35 changed files with 5499 additions and 1901 deletions

View File

@@ -0,0 +1,83 @@
import { POST } from '@/app/api/email/route';
import { NextRequest, NextResponse } from 'next/server';
import nodemailermock from '@/app/__tests__/__mocks__/nodemailer';
jest.mock('next/server', () => ({
NextResponse: {
json: jest.fn(),
},
}));
beforeEach(() => {
nodemailermock.mock.reset();
process.env.MY_EMAIL = 'test@dki.one';
process.env.MY_PASSWORD = 'test-password';
});
describe('POST /api/email', () => {
it('should send an email', async () => {
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: 'test@example.com',
name: 'Test User',
message: 'Hello!',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ message: 'Email sent' });
const sentEmails = nodemailermock.mock.getSentMail();
expect(sentEmails.length).toBe(1);
expect(sentEmails[0].to).toBe('test@dki.one');
expect(sentEmails[0].text).toBe('Hello!\n\n' + 'test@example.com');
});
it('should return an error if EMAIL or PASSWORD is missing', async () => {
delete process.env.MY_EMAIL;
delete process.env.MY_PASSWORD;
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: 'test@example.com',
name: 'Test User',
message: 'Hello!',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ error: 'Missing EMAIL or PASSWORD' }, { status: 500 });
});
it('should return an error if request body is invalid', async () => {
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: '',
name: 'Test User',
message: 'Test message',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ error: 'Invalid request body' }, { status: 400 });
});
it('should return an error if sending email fails', async () => {
nodemailermock.mock.setShouldFail(true);
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: 'test@example.com',
name: 'Test User',
message: 'Hello!',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ error: 'Failed to send email' }, { status: 500 });
});
});

View File

@@ -0,0 +1,57 @@
import { GET } from '@/app/api/fetchAllProjects/route';
import { NextResponse } from 'next/server';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/server', () => ({
NextResponse: {
json: jest.fn(),
},
}));
describe('GET /api/fetchAllProjects', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'some-key';
global.fetch = mockFetch({
posts: [
{
id: '67ac8dfa709c60000117d312',
title: 'Just Doing Some Testing',
meta_description: 'Hello bla bla bla bla',
slug: 'just-doing-some-testing',
updated_at: '2025-02-13T14:25:38.000+00:00',
},
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('should return a list of projects', async () => {
await GET();
expect(NextResponse.json).toHaveBeenCalledWith({
posts: [
{
id: '67ac8dfa709c60000117d312',
title: 'Just Doing Some Testing',
meta_description: 'Hello bla bla bla bla',
slug: 'just-doing-some-testing',
updated_at: '2025-02-13T14:25:38.000+00:00',
},
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
});

View File

@@ -0,0 +1,53 @@
import { GET } from '@/app/api/fetchImage/route';
import { NextRequest } from 'next/server';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch-img';
jest.mock('next/server', () => {
class NextResponseClass {
body: unknown;
init: unknown;
constructor(body: unknown, init?: unknown) {
this.body = body;
this.init = init;
}
static json(body: unknown, init?: unknown) {
return new NextResponseClass(body, init);
}
static from(body: unknown, init?: unknown) {
return new NextResponseClass(body, init);
}
}
return { NextResponse: NextResponseClass };
});
global.fetch = mockFetch({
ok: true,
headers: {
get: jest.fn().mockReturnValue('image/jpeg'),
},
arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(8)),
});
describe('GET /api/fetchImage', () => {
it('should return an error if no image URL is provided', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchImage',
} as unknown as NextRequest;
const response = await GET(mockRequest);
expect(response.body).toEqual({ error: 'Missing URL parameter' });
expect(response.init.status).toBe(400);
});
it('should fetch an image if URL is provided', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchImage?url=https://example.com/image.jpg',
} as unknown as NextRequest;
const response = await GET(mockRequest);
expect(response.body).toBeDefined();
expect(response.init.headers['Content-Type']).toBe('image/jpeg');
});
});

View File

@@ -0,0 +1,47 @@
import { GET } from '@/app/api/fetchProject/route';
import { NextRequest, NextResponse } from 'next/server';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/server', () => ({
NextResponse: {
json: jest.fn(),
},
}));
describe('GET /api/fetchProject', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'some-key';
global.fetch = mockFetch({
posts: [
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('should fetch a project by slug', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchProject?slug=blockchain-based-voting-system',
} as unknown as NextRequest;
await GET(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({
posts: [
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
});

View File

@@ -0,0 +1,14 @@
import { GET } from '@/app/api/og/route';
import { ImageResponse } from 'next/og';
jest.mock('next/og', () => ({
ImageResponse: jest.fn(),
}));
describe('GET /api/og', () => {
it('should return an Open Graph image', async () => {
await GET();
expect(ImageResponse).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,44 @@
import { GET } from '@/app/api/sitemap/route';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/server', () => ({
NextResponse: jest.fn().mockImplementation((body, init) => ({ body, init })),
}));
describe('GET /api/sitemap', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'test-api-key';
process.env.NEXT_PUBLIC_BASE_URL = 'https://dki.one';
global.fetch = mockFetch({
posts: [
{
id: '67ac8dfa709c60000117d312',
title: 'Just Doing Some Testing',
meta_description: 'Hello bla bla bla bla',
slug: 'just-doing-some-testing',
updated_at: '2025-02-13T14:25:38.000+00:00',
},
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('should return a sitemap', async () => {
const response = await GET();
expect(response.body).toContain('<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">');
expect(response.body).toContain('<loc>https://dki.one/</loc>');
expect(response.body).toContain('<loc>https://dki.one/legal-notice</loc>');
expect(response.body).toContain('<loc>https://dki.one/privacy-policy</loc>');
expect(response.body).toContain('<loc>https://dki.one/projects/just-doing-some-testing</loc>');
expect(response.body).toContain('<loc>https://dki.one/projects/blockchain-based-voting-system</loc>');
expect(response.init.headers['Content-Type']).toBe('application/xml');
});
});