-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
createTenant module alongside test setup
- Loading branch information
Showing
25 changed files
with
496 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
|
||
**/.classpath | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
NODE_ENV=production | ||
MANAGE_TENANTS=true | ||
POSTGRES_PASSWORD=postgres | ||
POSTGRES_DB=multiTenant_dev | ||
DATABASE_URL=postgres://postgres:postgres@postgres:5432/${POSTGRES_DB} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,36 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
# Comments are provided throughout this file to help you get started. | ||
# If you need more help, visit the Dockerfile reference guide at | ||
# https://docs.docker.com/engine/reference/builder/ | ||
|
||
ARG NODE_VERSION=20.10.0 | ||
ARG PNPM_VERSION=8.12.1 | ||
|
||
################################################################################ | ||
# Use node image for base image for all stages. | ||
FROM node:${NODE_VERSION}-alpine as base | ||
|
||
# Set working directory for all build stages. | ||
WORKDIR /usr/src/app | ||
|
||
# Install pnpm. | ||
RUN --mount=type=cache,target=/root/.npm \ | ||
npm install -g pnpm@${PNPM_VERSION} | ||
|
||
################################################################################ | ||
# Create a stage for installing production dependecies. | ||
FROM base as deps | ||
|
||
# Download dependencies as a separate step to take advantage of Docker's caching. | ||
# Leverage a cache mount to /root/.local/share/pnpm/store to speed up subsequent builds. | ||
# Leverage bind mounts to package.json and pnpm-lock.yaml to avoid having to copy them | ||
# into this layer. | ||
RUN --mount=type=bind,source=package.json,target=package.json \ | ||
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ | ||
--mount=type=cache,target=/root/.local/share/pnpm/store \ | ||
pnpm install --frozen-lockfile | ||
RUN npm install -g pnpm@${PNPM_VERSION} | ||
|
||
################################################################################ | ||
# Create a stage for building the application. | ||
FROM deps as build | ||
|
||
# Download additional development dependencies before building, as some projects require | ||
# "devDependencies" to be installed to build. If you don't need this, remove this step. | ||
# RUN --mount=type=bind,source=package.json,target=package.json \ | ||
# --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ | ||
# --mount=type=cache,target=/root/.local/share/pnpm/store \ | ||
# pnpm install --frozen-lockfile | ||
|
||
### Prisma section | ||
COPY prisma/schema.prisma ./prisma/ | ||
# Copy package.json so that package manager commands can be used. | ||
COPY package.json pnpm-lock.yaml ./ | ||
|
||
RUN pnpx prisma generate | ||
### | ||
RUN pnpm install --frozen-lockfile | ||
|
||
# Copy the rest of the source files into the image. | ||
COPY . . | ||
|
||
# RUN the prisma generate script. | ||
RUN pnpm prisma generate | ||
|
||
# Run the build script. | ||
RUN pnpm run build | ||
|
||
################################################################################ | ||
# Create a new stage to run the application with minimal runtime dependencies | ||
# where the necessary files are copied from the build stage. | ||
FROM base as final | ||
|
||
# Use production node environment by default. | ||
ENV NODE_ENV production | ||
|
||
# Run the application as a non-root user. | ||
USER node | ||
|
||
# Copy package.json so that package manager commands can be used. | ||
COPY package.json . | ||
|
||
# Copy the production dependencies from the deps stage and also | ||
# the built application from the build stage into the image. | ||
COPY --from=deps /usr/src/app/node_modules ./node_modules | ||
COPY --from=build /usr/src/app/dist ./dist | ||
|
||
# Expose the port that the application listens on. | ||
EXPOSE 5000 | ||
|
||
# Run the application. | ||
CMD pnpm start | ||
CMD pnpm start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
NODE_ENV=test | ||
MANAGE_TENANTS=true | ||
POSTGRES_PASSWORD=postgres | ||
POSTGRES_DB=multiTenant_test | ||
DATABASE_URL=postgres://postgres:postgres@testdb:5432/${POSTGRES_DB} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { executeQuery } from '../src/helpers'; | ||
|
||
export default async () => { | ||
// Drop test database if it exists | ||
await executeQuery('DROP DATABASE IF EXISTS "multiTenant_test";'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { executeQuery } from '../src/helpers'; | ||
|
||
export default async () => { | ||
await executeQuery('DROP DATABASE "multiTenant_test";'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { genId } from '../src/helpers'; | ||
import { faker } from '@faker-js/faker'; | ||
import { ITenant } from '../src/modules/types'; | ||
import { tenants } from '../src/modules/tenant'; | ||
|
||
let tenantID: string; | ||
let newTenant: ITenant; | ||
|
||
describe('tenants module', () => { | ||
describe('tenant without a set db_name parameter', () => { | ||
beforeEach(() => { | ||
// Create a new tenant | ||
newTenant = { | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
fqdn: faker.internet.domainName(), | ||
redirect_to: faker.string.nanoid(), | ||
force_https: faker.datatype.boolean(), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
// Dispose of the tenant Object | ||
newTenant = { | ||
fqdn: '', | ||
redirect_to: '', | ||
force_https: false, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}; | ||
}); | ||
|
||
it('should catch all unhandled errors', async () => { | ||
await expect(tenants.createTenant(newTenant)).rejects.toThrow( | ||
'Tenant object is empty', | ||
); | ||
}); | ||
|
||
it('should create a tenant and return the tenant ID', async () => { | ||
tenantID = await tenants.createTenant(newTenant); | ||
|
||
expect(tenantID).not.toBe(''); | ||
expect(tenantID).toBeDefined(); | ||
expect(tenantID).not.toBeNull(); | ||
expect(tenantID).not.toBeUndefined(); | ||
}); | ||
|
||
it('should throw an error if the tenant already exists', async () => { | ||
await expect(tenants.createTenant(newTenant)).rejects.toThrow( | ||
'Tenant already exists', | ||
); | ||
}); | ||
|
||
it('should throw an error if the tenant object is empty', async () => { | ||
await expect(tenants.createTenant({})).rejects.toThrow( | ||
'Tenant object is empty', | ||
); | ||
}); | ||
|
||
// Update Tenant Section | ||
it('should update a tenant', async () => { | ||
newTenant.db_name = genId(); | ||
newTenant.fqdn = faker.internet.domainName(); | ||
const result = await tenants.updateTenant(tenantID, newTenant); | ||
|
||
expect(result).not.toBe(''); | ||
expect(result).toBeDefined(); | ||
expect(result).not.toBeNull(); | ||
expect(result).not.toBeUndefined(); | ||
}); | ||
|
||
it('should throw an error if the tenant does not exist', async () => { | ||
await expect(tenants.updateTenant('null', newTenant)).rejects.toThrow( | ||
'Tenant does not exist', | ||
); | ||
}); | ||
|
||
it('should throw an error if the tenant ID is empty', async () => { | ||
await expect(tenants.updateTenant('', {})).rejects.toThrow( | ||
'Tenant ID is empty', | ||
); | ||
}); | ||
|
||
it('should throw an error if the tenant object is empty', async () => { | ||
await expect(tenants.createTenant(null, {})).rejects.toThrow( | ||
'Tenant object is empty', | ||
); | ||
}); | ||
|
||
// Delete Tenant Section | ||
it('should delete a tenant', async () => { | ||
const result = await tenants.deleteTenant(tenantID); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should throw an error if the tenant ID is empty', async () => { | ||
await expect(tenants.deleteTenant('')).rejects.toThrow( | ||
'Tenant ID is empty', | ||
); | ||
}); | ||
|
||
it('should throw an error if the tenant does not exist', async () => { | ||
await expect(tenants.deleteTenant('null')).rejects.toThrow( | ||
'Tenant does not exist', | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.