Skip to content

Commit

Permalink
Merge pull request #52 from DFanso/test/users
Browse files Browse the repository at this point in the history
Test/users
  • Loading branch information
DFanso authored Apr 30, 2024
2 parents c9a9c71 + 86b32fa commit b2d9471
Show file tree
Hide file tree
Showing 34 changed files with 940 additions and 390 deletions.
127 changes: 76 additions & 51 deletions .github/workflows/apiDeploy.yml
Original file line number Diff line number Diff line change
@@ -1,64 +1,89 @@
name: Deploy API TO VPS
name: API CI/CD

on:
pull_request:
branches:
- dev # Set this to your default branch
push:
branches:
- dev # Set this to your default branch
- dev # Set this to your default branch

jobs:
deploy:
CI:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'

- name: Install dependencies
run: |
cd API
npm ci
- name: Run tests
run: |
cd API
npm run test
CD:
needs: CI # Ensure the test job passes before deploying
runs-on: ubuntu-latest
if: github.event_name == 'push' # Only run on push events
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Build and Push Docker image
uses: docker/build-push-action@v2
with:
context: ./API
file: ./API/Dockerfile
push: true
tags: dfanso/quick-quest_backend:latest
build-args: |
MONGO_URI=${{ secrets.MONGO_URI }}
AWS_REGION=${{ secrets.AWS_REGION }}
AWS_BUCKET_NAME=${{ secrets.AWS_BUCKET_NAME }}
AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}
COGNITO_USER_POOL_ID=${{ secrets.COGNITO_USER_POOL_ID }}
COGNITO_CLIENT_ID=${{ secrets.COGNITO_CLIENT_ID }}
COGNITO_CLIENT_SECRET=${{ secrets.COGNITO_CLIENT_SECRET }}
PAYPAL_CLIENT_ID=${{ secrets.PAYPAL_CLIENT_ID }}
PAYPAL_CLIENT_SECRET=${{ secrets.PAYPAL_CLIENT_SECRET }}
FRONTEND_URL=${{ secrets.FRONTEND_URL }}
BREVO_SMTP=${{ secrets.BREVO_SMTP }}
BREVO_SMTP_PORT=${{ secrets.BREVO_SMTP_PORT }}
BREVO_USER=${{ secrets.BREVO_USER }}
BREVO_PASS=${{ secrets.BREVO_PASS }}
EMAIL_FROM_ADDRESS=${{ secrets.EMAIL_FROM_ADDRESS }}
PAYPAL_REDIRECT_URI=${{ secrets.PAYPAL_REDIRECT_URI }}
COGNITO_CALLBACK_URL=${{ secrets.COGNITO_CALLBACK_URL }}
COGNITO_DOMAIN=${{ secrets.COGNITO_DOMAIN }}
RECOMMENDATION_ENGINE_API=${{ secrets.RECOMMENDATION_ENGINE_API }}
- name: Build and Push Docker image
uses: docker/build-push-action@v2
with:
context: ./API
file: ./API/Dockerfile
push: true
tags: dfanso/quick-quest_backend:latest
build-args: |
MONGO_URI=${{ secrets.MONGO_URI }}
AWS_REGION=${{ secrets.AWS_REGION }}
AWS_BUCKET_NAME=${{ secrets.AWS_BUCKET_NAME }}
AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}
COGNITO_USER_POOL_ID=${{ secrets.COGNITO_USER_POOL_ID }}
COGNITO_CLIENT_ID=${{ secrets.COGNITO_CLIENT_ID }}
COGNITO_CLIENT_SECRET=${{ secrets.COGNITO_CLIENT_SECRET }}
PAYPAL_CLIENT_ID=${{ secrets.PAYPAL_CLIENT_ID }}
PAYPAL_CLIENT_SECRET=${{ secrets.PAYPAL_CLIENT_SECRET }}
FRONTEND_URL=${{ secrets.FRONTEND_URL }}
BREVO_SMTP=${{ secrets.BREVO_SMTP }}
BREVO_SMTP_PORT=${{ secrets.BREVO_SMTP_PORT }}
BREVO_USER=${{ secrets.BREVO_USER }}
BREVO_PASS=${{ secrets.BREVO_PASS }}
EMAIL_FROM_ADDRESS=${{ secrets.EMAIL_FROM_ADDRESS }}
PAYPAL_REDIRECT_URI=${{ secrets.PAYPAL_REDIRECT_URI }}
COGNITO_CALLBACK_URL=${{ secrets.COGNITO_CALLBACK_URL }}
COGNITO_DOMAIN=${{ secrets.COGNITO_DOMAIN }}
RECOMMENDATION_ENGINE_API=${{ secrets.RECOMMENDATION_ENGINE_API }}
- name: SSH and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
password: ${{ secrets.VPS_PASSWORD }}
script: |
docker pull dfanso/quick-quest_backend:latest
docker stop quick-quest_backend || true
docker rm quick-quest_backend || true
docker run -d --name quick-quest_backend -p 4000:9000 dfanso/quick-quest_backend:latest
- name: SSH and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
password: ${{ secrets.VPS_PASSWORD }}
script: |
docker pull dfanso/quick-quest_backend:latest
docker stop quick-quest_backend || true
docker rm quick-quest_backend || true
docker run -d --name quick-quest_backend -p 4000:9000 dfanso/quick-quest_backend:latest
43 changes: 43 additions & 0 deletions .github/workflows/buildCheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Next.js Build Check

on:
pull_request:
branches:
- dev # Set this to your default branch
paths:
- 'admin/**'
- 'customer/**'
- 'worker/**'

jobs:
build-check:
runs-on: ubuntu-latest
strategy:
matrix:
project: [admin, customer, worker]

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'

- name: Install Dependencies
run: |
cd ${{ matrix.project }}
npm ci
- name: Build Project
run: |
cd ${{ matrix.project }}
npm run build
- name: Export Build Artifact
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.project }}-build
path: ${{ matrix.project }}/.next/
if-no-files-found: error
20 changes: 0 additions & 20 deletions API/src/auth/auth.controller.spec.ts

This file was deleted.

18 changes: 0 additions & 18 deletions API/src/auth/auth.service.spec.ts

This file was deleted.

20 changes: 0 additions & 20 deletions API/src/bids/bids.controller.spec.ts

This file was deleted.

50 changes: 46 additions & 4 deletions API/src/bids/bids.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BidsService } from './bids.service';
import { Bid, BidDocument } from './entities/bid.entity';
import { CreateBidDto } from './dto/create-bid.dto';
import { UpdateBidDto } from './dto/update-bid.dto';
import { NotFoundException } from '@nestjs/common';

describe('BidsService', () => {
let service: BidsService;
Expand Down Expand Up @@ -42,9 +43,7 @@ describe('BidsService', () => {
};

const mockModel = {
create: jest.fn().mockResolvedValue({
save: jest.fn().mockResolvedValue(mockBid),
}),
create: jest.fn().mockResolvedValue(mockBid),
find: jest.fn().mockReturnValue(mockQuery),
findById: jest.fn().mockReturnValue({
populate: jest.fn().mockReturnThis(),
Expand All @@ -56,6 +55,9 @@ describe('BidsService', () => {
deleteOne: jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue({ deletedCount: 1 }),
}),
countDocuments: jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(15),
}),
};

const module: TestingModule = await Test.createTestingModule({
Expand Down Expand Up @@ -88,13 +90,53 @@ describe('BidsService', () => {
expect(service).toBeDefined();
});

describe('create', () => {
it('should create a new bid', async () => {
const createBidDto: CreateBidDto = {
customer: '647d9a6d7c9d44b9c6d9a6d8',
service: '647d9a6d7c9d44b9c6d9a6d9',
budget: 1000,
description: 'This is a bid description',
expireDate: new Date('2023-06-30T23:59:59.999Z'),
};

const result = await service.create(createBidDto);
expect(result).toEqual(mockBid);
});
});

describe('findMatchingBids', () => {
it('should find matching bids for a worker', async () => {
const result = await service.findMatchingBids(mockUser._id);
expect(result).toEqual([mockBid]);
});

it('should throw NotFoundException if worker is not found', async () => {
jest.spyOn(userService, 'findOne').mockResolvedValueOnce(null);

await expect(
service.findMatchingBids('nonexistentUserId'),
).rejects.toThrow(NotFoundException);
});
});

describe('findAll', () => {
it('should find all bids with pagination', async () => {
const result = await service.findAll(1, 10);
expect(result).toEqual({
bids: [mockBid],
totalPages: 2,
});
});

it('should find all bids for a specific customer', async () => {
const result = await service.findAll(1, 10, mockUser._id);
expect(result).toEqual({
bids: [mockBid],
totalPages: 2,
});
});
});

describe('findOne', () => {
it('should find a bid by id', async () => {
Expand Down Expand Up @@ -122,4 +164,4 @@ describe('BidsService', () => {
expect(result).toEqual({ deleted: true, id: mockBid._id });
});
});
});
});
4 changes: 2 additions & 2 deletions API/src/bids/bids.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class BidsService {
) {}

async create(createBidDto: CreateBidDto): Promise<Bid> {
const createdBid = new this.bidModel(createBidDto);
return createdBid.save();
const createdBid = await this.bidModel.create(createBidDto);
return createdBid;
}

async findMatchingBids(userId: string): Promise<Bid[]> {
Expand Down
20 changes: 0 additions & 20 deletions API/src/categories/categories.controller.spec.ts

This file was deleted.

Loading

0 comments on commit b2d9471

Please sign in to comment.