Skip to content

Commit 48a4149

Browse files
committed
Fix tests
1 parent cbd0ed9 commit 48a4149

File tree

8 files changed

+84
-59
lines changed

8 files changed

+84
-59
lines changed

.github/workflows/e2e.yml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
minio:
99
image: bitnami/minio:latest
1010
env:
11-
MINIO_ROOT_USER: minio
12-
MINIO_ROOT_PASSWORD: minio123
11+
MINIO_ROOT_USER: minioadmin
12+
MINIO_ROOT_PASSWORD: minioadmin
1313
ports:
1414
- 9000:9000
1515
steps:
@@ -35,26 +35,20 @@ jobs:
3535
with:
3636
args: s3api --endpoint-url http://minio:9000 create-bucket --bucket vh7-uploads
3737
env:
38-
AWS_ACCESS_KEY_ID: minio
39-
AWS_SECRET_ACCESS_KEY: minio123
38+
AWS_ACCESS_KEY_ID: minioadmin
39+
AWS_SECRET_ACCESS_KEY: minioadmin
4040
- name: Install dependencies
41-
run: yarn install --dev
41+
run: |
42+
yarn install --dev
43+
yarn global add wrangler
44+
- name: Migrate database
45+
working-directory: ./worker
46+
run: wrangler d1 migrations apply DB --local
4247
- name: Run API
4348
working-directory: ./worker
4449
run: |
45-
env > .env
46-
yarn run preview > ../worker.log 2>&1 &
50+
wrangler dev --port 8787 > ../worker.log 2>&1 &
4751
npx -y wait-on --timeout 30000 http-get://localhost:8787
48-
env:
49-
S3_ACCESS_KEY_ID: minio
50-
S3_SECRET_ACCESS_KEY: minio123
51-
S3_DEFAULT_REGION: eu-west-1
52-
S3_ENDPOINT_URL: http://localhost:9000
53-
S3_BUCKET: vh7-uploads
54-
VH7_ENV: testing
55-
SENTRY_DSN: ${{ secrets.WORKER_SENTRY_DSN }}
56-
SENTRY_CLIENT_ID: ${{ secrets.WORKER_SENTRY_CLIENT_ID }}
57-
SENTRY_CLIENT_SECRET: ${{ secrets.WORKER_SENTRY_CLIENT_SECRET }}
5852
- name: Build & Run App
5953
working-directory: ./app
6054
run: |

.github/workflows/worker.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ on:
55
jobs:
66
test:
77
runs-on: ubuntu-latest
8+
services:
9+
minio:
10+
image: bitnami/minio:latest
11+
env:
12+
MINIO_ROOT_USER: minioadmin
13+
MINIO_ROOT_PASSWORD: minioadmin
14+
ports:
15+
- 9000:9000
816
steps:
917
- name: Checkout
1018
uses: actions/checkout@v2
@@ -23,6 +31,13 @@ jobs:
2331
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
2432
restore-keys: |
2533
${{ runner.os }}-yarn-
34+
- name: Create S3 Bucket
35+
uses: giboow/action-aws-cli@v1
36+
with:
37+
args: s3api --endpoint-url http://minio:9000 create-bucket --bucket vh7-uploads
38+
env:
39+
AWS_ACCESS_KEY_ID: minioadmin
40+
AWS_SECRET_ACCESS_KEY: minioadmin
2641
- name: Install dependencies
2742
working-directory: ./worker
2843
run: |

app/cypress/e2e/view.cy.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
describe('View Page', () => {
33
it('can view a short link', () => {
44
// first, create a new short link
5-
cy.request('POST', 'http://localhost:8787/api/shorten', {
6-
url: 'http://localhost:3000/testing123'
5+
cy.request({
6+
method: 'POST',
7+
url: 'http://localhost:8787/api/shorten',
8+
form: true,
9+
body: {
10+
url: 'http://localhost:3000/testing123'
11+
}
712
}).then((res) => {
813
// then visit it
914
cy.visit(`/view/${res.body.id}`);
@@ -21,9 +26,14 @@ describe('View Page', () => {
2126
it('can view a paste', () => {
2227
cy.fixture('paste').then((code) => {
2328
// first, create a new paste
24-
cy.request('POST', 'http://localhost:8787/api/paste', {
25-
language: 'python',
26-
code
29+
cy.request({
30+
method: 'POST',
31+
url: 'http://localhost:8787/api/paste',
32+
form: true,
33+
body: {
34+
language: 'python',
35+
code: code,
36+
}
2737
}).then((res) => {
2838
// then visit it
2939
const id = res.body.id;

app/src/components/UploadForm.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { upload } from "../controller";
33
import { Dropzone } from '@mantine/dropzone';
44
import { Group, Text } from "@mantine/core";
55
import { Upload } from "react-feather";
6-
import { useForm } from "@mantine/hooks";
6+
import { useForm } from "@mantine/form";
77
import { AdvancedControls, initialValues, validationRules } from "./AdvancedControls";
88
import { CreateInfo } from "./CreateInfo";
99

app/src/controller.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,23 @@ function parseExpiry(expiryDays) {
3434
export async function shorten(url, expiryDays) {
3535
const expires = parseExpiry(expiryDays);
3636

37-
const res = await instance.post('/shorten', {
38-
url,
39-
expires
40-
});
37+
const form = new FormData();
38+
form.append('url', url);
39+
form.append('expires', expires);
40+
41+
const res = await instance.post('/shorten', form);
4142
return res.data;
4243
}
4344

4445
export async function paste(code, language, expiryDays) {
4546
const expires = parseExpiry(expiryDays);
4647

47-
const res = await instance.post('/paste', {
48-
code,
49-
language,
50-
expires
51-
});
48+
const form = new FormData();
49+
form.append('code', code);
50+
form.append('language', language);
51+
form.append('expires', expires);
52+
53+
const res = await instance.post('/paste', form);
5254
return res.data;
5355
}
5456

app/src/views/View.jsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ function View() {
4141
content = <Text mt={6}>Error: {data.error.message}. Please try again.</Text>;
4242
} else {
4343
switch (data.type) {
44-
case "url:1":
45-
title = shortUrl(data.data.url, 30);
44+
case "url":
45+
title = shortUrl(data.url, 30);
4646
content = <>
47-
<TimedRedirect href={data.data.url} />
47+
<TimedRedirect href={data.url} />
4848
</>;
4949
break;
50-
case "paste:1":
50+
case "paste":
5151
title = "Paste";
5252
content = <>
53-
<Prism language={data.data.language} id="paste-content">{data.data.code}</Prism>
53+
<Prism language={data.language} id="paste-content">{data.code}</Prism>
5454
<Button
5555
leftIcon={<Download size={16} />}
5656
component="a"
@@ -61,13 +61,13 @@ function View() {
6161
</Button>
6262
</>;
6363
break;
64-
case "upload:1":
65-
title = data.data.filename;
66-
let size = `${data.data.size} bytes`;
67-
if (data.data.size > 1000000) {
68-
size = `${(data.data.size / 1000000).toFixed(1)} MB`
69-
} else if (data.data.size > 1000) {
70-
size = `${(data.data.size / 1000).toFixed(1)} KB`
64+
case "upload":
65+
title = data.filename;
66+
let size = `${data.size} bytes`;
67+
if (data.size > 1000000) {
68+
size = `${(data.size / 1000000).toFixed(1)} MB`
69+
} else if (data.size > 1000) {
70+
size = `${(data.size / 1000).toFixed(1)} KB`
7171
}
7272

7373
content = <>
@@ -78,7 +78,7 @@ function View() {
7878

7979
<Text style={{ overflowX: "auto" }}>
8080
<ul>
81-
<li><b>SHA256 Hash:</b> <span id="upload-sha256">{data.data.hash}</span></li>
81+
<li><b>SHA256 Hash:</b> <span id="upload-sha256">{data.hash}</span></li>
8282
<li><b>File Size:</b> {size}</li>
8383
</ul>
8484
</Text>
@@ -88,12 +88,12 @@ function View() {
8888
component="a"
8989
href={`${baseURL}${link}?direct=1`}
9090
>
91-
Download {data.data.filename}
91+
Download {data.filename}
9292
</Button>
9393
</>;
9494
break;
9595
}
96-
subtitle = `Created ${(new Date(data.created).toLocaleDateString())}`;
96+
subtitle = `Created ${(new Date(data.createdAt).toLocaleDateString())}`;
9797
}
9898
}
9999

worker/src/index.spec.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { getBindingsProxy } from 'wrangler';
2-
import { readFile } from 'fs/promises';
3-
import path from 'path';
42
import { drizzle } from 'drizzle-orm/d1';
53
import app, { type Bindings } from './index';
64
import * as models from './models';
@@ -11,11 +9,11 @@ const { bindings } = await getBindingsProxy();
119
const appEnv: Bindings = {
1210
DB: bindings.DB as D1Database,
1311
VH7_ENV: 'testing',
14-
S3_ACCESS_KEY_ID: 'minioadmin',
15-
S3_SECRET_ACCESS_KEY: 'minioadmin',
16-
S3_REGION: 'eu-west-1',
17-
S3_ENDPOINT_URL: 'http://localhost:9000',
18-
S3_BUCKET: 'vh7-uploads',
12+
S3_ACCESS_KEY_ID: process.env.S3_ACCESS_KEY_ID || 'minioadmin',
13+
S3_SECRET_ACCESS_KEY: process.env.S3_SECRET_ACCESS_KEY || 'minioadmin',
14+
S3_REGION: process.env.S3_REGION || 'eu-west-1',
15+
S3_ENDPOINT_URL: process.env.S3_ENDPOINT_URL || 'http://localhost:9000',
16+
S3_BUCKET: process.env.S3_BUCKET || 'vh7-uploads',
1917
};
2018

2119
beforeAll(async () => {
@@ -117,9 +115,10 @@ describe('API', () => {
117115
});
118116

119117
test('upload', async () => {
120-
const filename = path.join(__dirname, '../../app/public/assets/favicon.ico');
121118
const data = new FormData();
122-
data.append('file', new Blob([await readFile(filename)]), 'favicon.ico');
119+
data.append('file', new Blob(['Hello, World!'], {
120+
type: 'text/plain',
121+
}), 'test.txt');
123122

124123
const res = await app.request('http://vh7.uk/api/upload', {
125124
method: 'POST',
@@ -132,9 +131,9 @@ describe('API', () => {
132131
expect.objectContaining({
133132
id: expect.any(String),
134133
type: 'upload',
135-
filename: 'favicon.ico',
136-
size: 15406,
137-
hash: 'b8eea8c82be6c275d8f664b5cafc5b5081e240e4f1826aa57f068606a1c8741f',
134+
filename: 'test.txt',
135+
size: 13,
136+
hash: 'dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f',
138137
createdAt: expect.any(String),
139138
expiresAt: expect.any(String),
140139
updatedAt: expect.any(String),

worker/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono, type MiddlewareHandler } from 'hono';
22
import { drizzle, type DrizzleD1Database } from 'drizzle-orm/d1';
3+
import { cors } from 'hono/cors';
34
import { PasteArgs, ShortLinkArgs, UploadArgs } from './schema';
45
import {
56
createPaste, createShortUrl, createUpload, lookup,
@@ -32,6 +33,10 @@ const withDb: MiddlewareHandler = async (c, next) => {
3233
await next();
3334
};
3435

36+
app.use('*', cors());
37+
38+
app.get('/', (c) => c.text('VH7'));
39+
3540
app.post('/api/shorten',
3641
withDb,
3742
async (c) => {

0 commit comments

Comments
 (0)