Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-walker committed Jan 27, 2024
1 parent cbd0ed9 commit 48a4149
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 59 deletions.
28 changes: 11 additions & 17 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
minio:
image: bitnami/minio:latest
env:
MINIO_ROOT_USER: minio
MINIO_ROOT_PASSWORD: minio123
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: minioadmin
ports:
- 9000:9000
steps:
Expand All @@ -35,26 +35,20 @@ jobs:
with:
args: s3api --endpoint-url http://minio:9000 create-bucket --bucket vh7-uploads
env:
AWS_ACCESS_KEY_ID: minio
AWS_SECRET_ACCESS_KEY: minio123
AWS_ACCESS_KEY_ID: minioadmin
AWS_SECRET_ACCESS_KEY: minioadmin
- name: Install dependencies
run: yarn install --dev
run: |
yarn install --dev
yarn global add wrangler
- name: Migrate database
working-directory: ./worker
run: wrangler d1 migrations apply DB --local
- name: Run API
working-directory: ./worker
run: |
env > .env
yarn run preview > ../worker.log 2>&1 &
wrangler dev --port 8787 > ../worker.log 2>&1 &
npx -y wait-on --timeout 30000 http-get://localhost:8787
env:
S3_ACCESS_KEY_ID: minio
S3_SECRET_ACCESS_KEY: minio123
S3_DEFAULT_REGION: eu-west-1
S3_ENDPOINT_URL: http://localhost:9000
S3_BUCKET: vh7-uploads
VH7_ENV: testing
SENTRY_DSN: ${{ secrets.WORKER_SENTRY_DSN }}
SENTRY_CLIENT_ID: ${{ secrets.WORKER_SENTRY_CLIENT_ID }}
SENTRY_CLIENT_SECRET: ${{ secrets.WORKER_SENTRY_CLIENT_SECRET }}
- name: Build & Run App
working-directory: ./app
run: |
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ on:
jobs:
test:
runs-on: ubuntu-latest
services:
minio:
image: bitnami/minio:latest
env:
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: minioadmin
ports:
- 9000:9000
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -23,6 +31,13 @@ jobs:
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Create S3 Bucket
uses: giboow/action-aws-cli@v1
with:
args: s3api --endpoint-url http://minio:9000 create-bucket --bucket vh7-uploads
env:
AWS_ACCESS_KEY_ID: minioadmin
AWS_SECRET_ACCESS_KEY: minioadmin
- name: Install dependencies
working-directory: ./worker
run: |
Expand Down
20 changes: 15 additions & 5 deletions app/cypress/e2e/view.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
describe('View Page', () => {
it('can view a short link', () => {
// first, create a new short link
cy.request('POST', 'http://localhost:8787/api/shorten', {
url: 'http://localhost:3000/testing123'
cy.request({
method: 'POST',
url: 'http://localhost:8787/api/shorten',
form: true,
body: {
url: 'http://localhost:3000/testing123'
}
}).then((res) => {
// then visit it
cy.visit(`/view/${res.body.id}`);
Expand All @@ -21,9 +26,14 @@ describe('View Page', () => {
it('can view a paste', () => {
cy.fixture('paste').then((code) => {
// first, create a new paste
cy.request('POST', 'http://localhost:8787/api/paste', {
language: 'python',
code
cy.request({
method: 'POST',
url: 'http://localhost:8787/api/paste',
form: true,
body: {
language: 'python',
code: code,
}
}).then((res) => {
// then visit it
const id = res.body.id;
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/UploadForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { upload } from "../controller";
import { Dropzone } from '@mantine/dropzone';
import { Group, Text } from "@mantine/core";
import { Upload } from "react-feather";
import { useForm } from "@mantine/hooks";
import { useForm } from "@mantine/form";
import { AdvancedControls, initialValues, validationRules } from "./AdvancedControls";
import { CreateInfo } from "./CreateInfo";

Expand Down
20 changes: 11 additions & 9 deletions app/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ function parseExpiry(expiryDays) {
export async function shorten(url, expiryDays) {
const expires = parseExpiry(expiryDays);

const res = await instance.post('/shorten', {
url,
expires
});
const form = new FormData();
form.append('url', url);
form.append('expires', expires);

const res = await instance.post('/shorten', form);
return res.data;
}

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

const res = await instance.post('/paste', {
code,
language,
expires
});
const form = new FormData();
form.append('code', code);
form.append('language', language);
form.append('expires', expires);

const res = await instance.post('/paste', form);
return res.data;
}

Expand Down
30 changes: 15 additions & 15 deletions app/src/views/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ function View() {
content = <Text mt={6}>Error: {data.error.message}. Please try again.</Text>;
} else {
switch (data.type) {
case "url:1":
title = shortUrl(data.data.url, 30);
case "url":
title = shortUrl(data.url, 30);
content = <>
<TimedRedirect href={data.data.url} />
<TimedRedirect href={data.url} />
</>;
break;
case "paste:1":
case "paste":
title = "Paste";
content = <>
<Prism language={data.data.language} id="paste-content">{data.data.code}</Prism>
<Prism language={data.language} id="paste-content">{data.code}</Prism>
<Button
leftIcon={<Download size={16} />}
component="a"
Expand All @@ -61,13 +61,13 @@ function View() {
</Button>
</>;
break;
case "upload:1":
title = data.data.filename;
let size = `${data.data.size} bytes`;
if (data.data.size > 1000000) {
size = `${(data.data.size / 1000000).toFixed(1)} MB`
} else if (data.data.size > 1000) {
size = `${(data.data.size / 1000).toFixed(1)} KB`
case "upload":
title = data.filename;
let size = `${data.size} bytes`;
if (data.size > 1000000) {
size = `${(data.size / 1000000).toFixed(1)} MB`
} else if (data.size > 1000) {
size = `${(data.size / 1000).toFixed(1)} KB`
}

content = <>
Expand All @@ -78,7 +78,7 @@ function View() {

<Text style={{ overflowX: "auto" }}>
<ul>
<li><b>SHA256 Hash:</b> <span id="upload-sha256">{data.data.hash}</span></li>
<li><b>SHA256 Hash:</b> <span id="upload-sha256">{data.hash}</span></li>
<li><b>File Size:</b> {size}</li>
</ul>
</Text>
Expand All @@ -88,12 +88,12 @@ function View() {
component="a"
href={`${baseURL}${link}?direct=1`}
>
Download {data.data.filename}
Download {data.filename}
</Button>
</>;
break;
}
subtitle = `Created ${(new Date(data.created).toLocaleDateString())}`;
subtitle = `Created ${(new Date(data.createdAt).toLocaleDateString())}`;
}
}

Expand Down
23 changes: 11 additions & 12 deletions worker/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { getBindingsProxy } from 'wrangler';
import { readFile } from 'fs/promises';
import path from 'path';
import { drizzle } from 'drizzle-orm/d1';
import app, { type Bindings } from './index';
import * as models from './models';
Expand All @@ -11,11 +9,11 @@ const { bindings } = await getBindingsProxy();
const appEnv: Bindings = {
DB: bindings.DB as D1Database,
VH7_ENV: 'testing',
S3_ACCESS_KEY_ID: 'minioadmin',
S3_SECRET_ACCESS_KEY: 'minioadmin',
S3_REGION: 'eu-west-1',
S3_ENDPOINT_URL: 'http://localhost:9000',
S3_BUCKET: 'vh7-uploads',
S3_ACCESS_KEY_ID: process.env.S3_ACCESS_KEY_ID || 'minioadmin',
S3_SECRET_ACCESS_KEY: process.env.S3_SECRET_ACCESS_KEY || 'minioadmin',
S3_REGION: process.env.S3_REGION || 'eu-west-1',
S3_ENDPOINT_URL: process.env.S3_ENDPOINT_URL || 'http://localhost:9000',
S3_BUCKET: process.env.S3_BUCKET || 'vh7-uploads',
};

beforeAll(async () => {
Expand Down Expand Up @@ -117,9 +115,10 @@ describe('API', () => {
});

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

const res = await app.request('http://vh7.uk/api/upload', {
method: 'POST',
Expand All @@ -132,9 +131,9 @@ describe('API', () => {
expect.objectContaining({
id: expect.any(String),
type: 'upload',
filename: 'favicon.ico',
size: 15406,
hash: 'b8eea8c82be6c275d8f664b5cafc5b5081e240e4f1826aa57f068606a1c8741f',
filename: 'test.txt',
size: 13,
hash: 'dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f',
createdAt: expect.any(String),
expiresAt: expect.any(String),
updatedAt: expect.any(String),
Expand Down
5 changes: 5 additions & 0 deletions worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Hono, type MiddlewareHandler } from 'hono';
import { drizzle, type DrizzleD1Database } from 'drizzle-orm/d1';
import { cors } from 'hono/cors';
import { PasteArgs, ShortLinkArgs, UploadArgs } from './schema';
import {
createPaste, createShortUrl, createUpload, lookup,
Expand Down Expand Up @@ -32,6 +33,10 @@ const withDb: MiddlewareHandler = async (c, next) => {
await next();
};

app.use('*', cors());

app.get('/', (c) => c.text('VH7'));

app.post('/api/shorten',
withDb,
async (c) => {
Expand Down

0 comments on commit 48a4149

Please sign in to comment.