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,12 @@
export function mockFetch(data: Record<string, unknown>) {
return jest.fn().mockImplementation(() =>
Promise.resolve({
ok: true,
headers: {
get: jest.fn().mockReturnValue('image/jpeg'),
},
arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(8)),
json: () => data,
}),
);
}

View File

@@ -0,0 +1,12 @@
export function mockFetch(responseText: string) {
return jest.fn().mockImplementation((url: string) => {
// Check if the URL being requested is our sitemap endpoint.
if (url.includes('/api/sitemap')) {
return Promise.resolve({
ok: true,
text: () => Promise.resolve(responseText),
});
}
return Promise.reject(new Error(`Unknown URL: ${url}`));
});
}

View File

@@ -0,0 +1,8 @@
export function mockFetch(data: Record<string, unknown>) {
return jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => Promise.resolve(data),
});
});
}

View File

@@ -0,0 +1,9 @@
/**
* Jest Mock
* ./__mocks__/nodemailer.js
**/
import nodemailer from 'nodemailer';
import { getMockFor } from 'nodemailer-mock';
const nodemailermock = getMockFor(nodemailer);
export default nodemailermock;

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');
});
});

View File

@@ -0,0 +1,29 @@
import { render, screen, fireEvent } from '@testing-library/react';
import Contact from '@/app/components/Contact';
import '@testing-library/jest-dom';
// Mock the fetch function
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ message: 'Email sent' }),
})
) as jest.Mock;
describe('Contact', () => {
it('renders the contact form', () => {
render(<Contact />);
expect(screen.getByPlaceholderText('Name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Email')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Message')).toBeInTheDocument();
});
it('submits the form', async () => {
render(<Contact />);
fireEvent.change(screen.getByPlaceholderText('Name'), { target: { value: 'John Doe' } });
fireEvent.change(screen.getByPlaceholderText('Email'), { target: { value: 'john@example.com' } });
fireEvent.change(screen.getByPlaceholderText('Message'), { target: { value: 'Hello!' } });
fireEvent.click(screen.getByText('Send'));
expect(await screen.findByText('Email sent')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import Footer from '@/app/components/Footer';
import '@testing-library/jest-dom';
describe('Footer', () => {
it('renders the footer', () => {
render(<Footer />);
expect(screen.getByText('Connect with me on social platforms:')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,28 @@
import { render, screen } from '@testing-library/react';
import Header from '@/app/components/Header';
import '@testing-library/jest-dom';
describe('Header', () => {
it('renders the header', () => {
render(<Header />);
expect(screen.getByText('Dennis Konkol')).toBeInTheDocument();
const aboutButtons = screen.getAllByText('About');
expect(aboutButtons.length).toBeGreaterThan(0);
const projectsButtons = screen.getAllByText('Projects');
expect(projectsButtons.length).toBeGreaterThan(0);
const contactButtons = screen.getAllByText('Contact');
expect(contactButtons.length).toBeGreaterThan(0);
});
it('renders the mobile header', () => {
render(<Header />);
const openMenuButton = screen.getByLabelText('Open menu');
expect(openMenuButton).toBeInTheDocument();
const closeMenuButton = screen.getByLabelText('Close menu');
expect(closeMenuButton).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,15 @@
import { render, screen } from '@testing-library/react';
import Hero from '@/app/components/Hero';
import '@testing-library/jest-dom';
describe('Hero', () => {
it('renders the hero section', () => {
render(<Hero />);
expect(screen.getByText('Hi, Im Dennis')).toBeInTheDocument();
expect(screen.getByText('Student & Software Engineer')).toBeInTheDocument();
expect(screen.getByText('Based in Osnabrück, Germany')).toBeInTheDocument();
expect(screen.getByText('Passionate about technology, coding, and solving real-world problems. I enjoy building innovative solutions and continuously expanding my knowledge.')).toBeInTheDocument();
expect(screen.getByText('Currently working on exciting projects that merge creativity with functionality. Always eager to learn and collaborate!')).toBeInTheDocument();
expect(screen.getByAltText('Image of Dennis')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,44 @@
import { render, screen, waitFor } from '@testing-library/react';
import Projects from '@/app/components/Projects';
import '@testing-library/jest-dom';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
describe('Projects', () => {
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('renders the projects section', async () => {
render(<Projects />);
expect(await screen.findByText('Projects')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByText('Just Doing Some Testing')).toBeInTheDocument();
expect(screen.getByText('Hello bla bla bla bla')).toBeInTheDocument();
expect(screen.getByText('Blockchain Based Voting System')).toBeInTheDocument();
expect(screen.getByText('This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.')).toBeInTheDocument();
expect(screen.getByText('More to come')).toBeInTheDocument();
});
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import LegalNotice from '@/app/legal-notice/page';
import '@testing-library/jest-dom';
describe('LegalNotice', () => {
it('renders the legal notice page', () => {
render(<LegalNotice />);
expect(screen.getByText('Impressum')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import NotFound from '@/app/not-found';
import '@testing-library/jest-dom';
describe('NotFound', () => {
it('renders the 404 page', () => {
render(<NotFound />);
expect(screen.getByText("Oops! The page you're looking for doesn't exist.")).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import Home from '@/app/page';
import '@testing-library/jest-dom';
describe('Home', () => {
it('renders the home page', () => {
render(<Home />);
expect(screen.getByText('Hi, Im Dennis')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import PrivacyPolicy from '@/app/privacy-policy/page';
import '@testing-library/jest-dom';
describe('PrivacyPolicy', () => {
it('renders the privacy policy page', () => {
render(<PrivacyPolicy />);
expect(screen.getByText('Datenschutzerklärung')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,45 @@
import { render, screen, waitFor } from '@testing-library/react';
import ProjectDetails from '@/app/projects/[slug]/page';
import '@testing-library/jest-dom';
import { useRouter, useSearchParams, useParams, usePathname } from 'next/navigation';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/navigation', () => ({
useRouter: jest.fn(),
useSearchParams: jest.fn(),
useParams: jest.fn(),
usePathname: jest.fn(),
}));
describe('ProjectDetails', () => {
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',
description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
html: '<p>This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.</p>',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('renders the project details page', async () => {
(useRouter as jest.Mock).mockReturnValue({});
(useSearchParams as jest.Mock).mockReturnValue(new URLSearchParams());
(useParams as jest.Mock).mockReturnValue({ slug: 'blockchain-based-voting-system' });
(usePathname as jest.Mock).mockReturnValue('/projects/blockchain-based-voting-system');
render(<ProjectDetails />);
await waitFor(() => {
expect(screen.getByText('Blockchain Based Voting System')).toBeInTheDocument();
expect(screen.getByText('This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.')).toBeInTheDocument();
});
});
});

View File

@@ -0,0 +1,44 @@
import '@testing-library/jest-dom';
import { GET } from '@/app/sitemap.xml/route';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch-sitemap';
jest.mock('next/server', () => ({
NextResponse: jest.fn().mockImplementation((body, init) => ({ body, init })),
}));
describe('Sitemap Component', () => {
beforeAll(() => {
process.env.NEXT_PUBLIC_BASE_URL = 'https://dki.one';
global.fetch = mockFetch(`
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://dki.one/</loc>
</url>
<url>
<loc>https://dki.one/legal-notice</loc>
</url>
<url>
<loc>https://dki.one/privacy-policy</loc>
</url>
<url>
<loc>https://dki.one/projects/just-doing-some-testing</loc>
</url>
<url>
<loc>https://dki.one/projects/blockchain-based-voting-system</loc>
</url>
</urlset>
`);
});
it('should render the sitemap XML', 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');
});
});