diff --git a/.gitignore b/.gitignore index 8744d35..c87577c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ post-backup.sql user-backup.sql .env +auth-svc/.env.test.docker diff --git a/auth-svc/compose.test.yaml b/auth-svc/compose.test.yaml new file mode 100644 index 0000000..e3009f0 --- /dev/null +++ b/auth-svc/compose.test.yaml @@ -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: + + + \ No newline at end of file diff --git a/auth-svc/dockerfile b/auth-svc/dockerfile index 397c7f1..5b39c1d 100644 --- a/auth-svc/dockerfile +++ b/auth-svc/dockerfile @@ -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 diff --git a/auth-svc/package.json b/auth-svc/package.json index ac6360e..fdae634 100644 --- a/auth-svc/package.json +++ b/auth-svc/package.json @@ -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": "", diff --git a/auth-svc/tests/api.test.mjs b/auth-svc/tests/api.test.mjs index e2122cc..1682541 100644 --- a/auth-svc/tests/api.test.mjs +++ b/auth-svc/tests/api.test.mjs @@ -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); +});