Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
post-backup.sql
user-backup.sql
.env
auth-svc/.env.test.docker
60 changes: 60 additions & 0 deletions auth-svc/compose.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
services:
build-test:
build:
context: .
target: test
command: ["bun", "run", "prisma-dev", "migrate", "dev"]
environment:
- DOTENV_PRIVATE_KEY_DEVELOPMENT
networks:
- testauth
depends_on:
db-authsvc:
condition: service_healthy


test:
build:
context: .
target: test
environment:
- DOTENV_PRIVATE_KEY_DEVELOPMENT
networks:
- testauth
depends_on:
build-test:
condition: service_completed_successfully
develop:
watch:
- path: .
ignore:
- ./node_modules
- ./package.json
action: sync
target: /auth-svc/
- path: ./package.json
action: rebuild

db-authsvc:
image: postgres:alpine

env_file:
- ./.env.test.docker
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 3s
timeout: 3s
retries: 2
start_period: 3s
networks:
- testauth
volumes:
- test:/var/lib/postgresql

volumes:
test:
networks:
testauth:



9 changes: 9 additions & 0 deletions auth-svc/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ COPY package.json bun.lock /temp/prod
RUN --mount=type=cache,id=global-bun-cache,target=/root/.bun/install/cache \
cd /temp/prod && bun install --frozen-lockfile --production

FROM base AS test
RUN mkdir /auth-svc
WORKDIR /auth-svc
COPY --from=install /temp/dev/node_modules node_modules
COPY . .
RUN bunx --bun prisma --config=./src/prisma.config.ts generate
EXPOSE 5859
CMD [ "bun", "run", "test" ]

FROM base AS prerelease
RUN mkdir /auth-svc
WORKDIR /auth-svc
Expand Down
2 changes: 1 addition & 1 deletion auth-svc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prisma-dev": "dotenvx run -f .env.development -- bunx --bun prisma --config=./src/prisma.config.ts",
"app": "dotenvx run -f .env.development -- bun ./src/app.mjs",
"build": "bunx --bun prisma --config=./src/prisma.config.ts generate && tsc",
"test": "dotenvx run -f .env.development -- bun test"
"test": "dotenvx run -f .env.development -- bun test --watch"
},
"author": "Usman Ghani",
"license": "",
Expand Down
111 changes: 100 additions & 11 deletions auth-svc/tests/api.test.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,107 @@


import { sign } from "hono/jwt";
import { describe, test, expect } from "bun:test";
import { app } from "../src/app.mjs";
import bcrypt from "bcryptjs";

const token = await sign(
`${process.env.APIKEY_USER}:${process.env.APIKEY_PASSWORD}`,
process.env.BEARER_TOKEN_PRIVATEKEY,
"HS256",
);

const token = await sign(`${process.env.APIKEY_USER}:${process.env.APIKEY_PASSWORD}`, process.env.BEARER_TOKEN_PRIVATEKEY, "HS256");
console.log(token)
test("/list", async () => {
test("should pass to /list", async () => {
const res = await app.request("/list", {
method: "GET",
headers: {
"Authorization": `Bearer ${token}`
}
})
console.log(await res.text())
expect(res.status).toBe(200)
})
Authorization: `Bearer ${token}`,
},
});
;
expect(res.status).toBe(200);
});

test("should pass to /create-user", async () => {
const passhash = await await bcrypt.hash("test", 10)
const res = await app.request("/create-user", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},

body: JSON.stringify({
username: "testUser",
password_hash: passhash,
fullName: "Test User",
firstName: "Test",
lastName: "User",
email: "test@test.com",
}),
});

expect(res.status).toBe(200);
});
test("should pass to /find-or-create find first user", async () => {
const passhash = await await bcrypt.hash("test", 10)
const res = await app.request("/find-or-create", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},

body: JSON.stringify({
username: "testUser",
password_hash: passhash,
fullName: "Test User",
firstName: "Test",
lastName: "User",
email: "test@test.com",
}),
});

expect(res.status).toBe(200);
});

test("should pass to /create-user 2nd user", async () => {
const passhash = await await bcrypt.hash("test", 10)
const res = await app.request("/create-user", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},

body: JSON.stringify({
username: "testuser2",
password_hash: passhash,
fullName: "Test User 2",
firstName: "Test2",
lastName: "User2",
email: "test2@test.com",
}),
});
expect(res.status).toBe(200);
});

test("should pass to /destroy", async () => {
const res = await app.request("/destroy/testUser", {
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
},
});
;
expect(res.status).toBe(200);
});

test("should pass to /destroy 2nd user", async () => {
const res = await app.request("/destroy/testuser2", {
method: "DELETE",
headers: {
Authorization: `Bearer ${token}`,
},
});
;
expect(res.status).toBe(200);
});
Loading