Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6f107fa
testing jest and supertest
shimupan Oct 15, 2024
1166caa
Prettify Code: src/api/__test__/getUserTest.js
actions-user Oct 15, 2024
fe56689
test
shimupan Oct 18, 2024
80f3fbf
Merge branch 'main' of github.com:shimupan/lineupx into 364-writing-t…
shimupan Oct 18, 2024
7d86cc9
Merge branch '364-writing-tests-for-api-endpoints' of github.com:shim…
shimupan Oct 18, 2024
c7b52e2
Prettify Code: src/jest.config.js
actions-user Oct 18, 2024
e07b384
testing
shimupan Oct 22, 2024
74e4ba6
Merge branch '364-writing-tests-for-api-endpoints' of github.com:shim…
shimupan Oct 22, 2024
8a95c35
Merge branch 'main' of github.com:shimupan/lineupx into 364-writing-t…
shimupan Oct 22, 2024
5303f27
Merge branch 'main' of github.com:shimupan/lineupx into 364-writing-t…
shimupan Oct 24, 2024
2791d2a
reset
shimupan Oct 24, 2024
4b5bf1c
testing changes
shimupan Oct 25, 2024
daa94b3
Prettify Code: src/api/__test__/user.spec.js
actions-user Oct 25, 2024
504f36a
rm'd cd
shimupan Oct 25, 2024
a2bf988
added vitest install
shimupan Oct 25, 2024
8e5c441
changed to npm install
shimupan Oct 25, 2024
94cab28
testing
shimupan Oct 25, 2024
c7b070a
Merge branch 'main' of github.com:shimupan/lineupx into 364-writing-t…
shimupan Nov 8, 2024
ced0d57
test
shimupan Nov 8, 2024
2a16eb8
test
shimupan Nov 8, 2024
e9f5a1d
testing without docker
shimupan Nov 8, 2024
377c0f9
testing docker again
shimupan Nov 12, 2024
e90a0f2
reset
shimupan Nov 12, 2024
2e36ea1
run setup first
shimupan Nov 12, 2024
bd44979
checking with docker
shimupan Nov 12, 2024
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
47 changes: 16 additions & 31 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ jobs:
steps:
- name: Checkout Repository
uses: actions/checkout@v3

- name: Run Setup Script
run: |
chmod +x scripts/setup.sh
./scripts/setup.sh
run: sh ./scripts/setup.sh

- name: Build API Image
run: docker build -t api ./src/api
Expand All @@ -57,31 +55,18 @@ jobs:

- name: Check Docker Compose Logs
run: docker compose logs

- name: Save Docker Compose Output
id: services
run: echo "containers_started=true" >> $GITHUB_ENV

api_test:
runs-on: ubuntu-latest
needs: build_app

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

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

- name: Install dependencies
run: npm install
working-directory: src/api

- name: Wait for DB
if: env.containers_started == 'true'

- name: Set API directory permissions
run: |
while ! docker compose logs lineupx_api | grep -q "database connected"; do
sleep 5
done
sudo chown -R $USER:$USER ./src/api
sudo chmod -R 755 ./src/api

- name: Install Vitest
run: npm install -D vitest
working-directory: ./src/api

- name: Run Tests
env:
MONGO_URI: mongodb://admin:admin@lineupx_db:27017/LineupX
run: npm run test:health
working-directory: ./src/api
28 changes: 2 additions & 26 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,14 @@ jobs:
prettier:
runs-on: ubuntu-latest

permissions:
contents: write

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}

- name: Prettify Code
- name: Check Code Formatting
run: |
npm install
npx prettier --write "src/**/*.{js,jsx,ts,tsx,json,css,md}"

- name: Commit changes
if: success()
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add .
FILES=$(git diff --name-only --staged)
if [ -z "$FILES" ]; then
echo "No changes to commit."
exit 0
else
git commit -m "Prettify Code: $FILES"
fi

- name: Push changes
if: success()
uses: ad-m/github-push-action@v0.6.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.head_ref }}
npx prettier --check "src/**/*.{js,jsx,ts,tsx,json,css,md}"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ node_modules

.vscode

dist
dist

coverage
12 changes: 12 additions & 0 deletions src/api/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
1 change: 0 additions & 1 deletion src/api/__test__/getUserTest.js

This file was deleted.

44 changes: 44 additions & 0 deletions src/api/__test__/init.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, it, expect, beforeAll } from 'vitest';
import request from 'supertest';
import mongoose from 'mongoose';
import app from '../setupServer';

const req = request(app);

const waitForConnection = async () => {
let attempts = 0;
while (attempts < 5 && mongoose.connection.readyState !== 1) {
await new Promise(resolve => setTimeout(resolve, 1000));
attempts++;
}
};

describe('Testing Home Page', () => {
beforeAll(async () => {
await waitForConnection();
});

it('Server Should Be Running', async () => {
const res = await req.get('/');
expect(res.status).toBe(200);
expect(res.body.data).toBe('server is running');
});
});

describe('Checking For Server Health', () => {
let res;

beforeAll(async () => {
await waitForConnection();
res = await req.get('/health');
});

it('Should Report Back Positive', async () => {
expect(res.status).toBe(200);
expect(res.body.status).toBe('OK');
expect(res.body.message).toBe('Server is running');
expect(res.body.dbStatus).toBe('Connected');
expect(res.body).toHaveProperty('timestamp');
expect(res.body).toHaveProperty('uptime');
});
});
91 changes: 91 additions & 0 deletions src/api/__test__/user.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { describe, it, expect, beforeAll } from 'vitest';
import request from 'supertest';
import app from '../setupServer';

const req = request(app);

const user1 = 'ooccupate';
const user2 = 'kagiriez';
const unknownUser = 'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk';
const defaultParams = ['followers', 'saved', 'email'];

describe('Route: /user/:id -- Simulate an User Visiting Their Own Profile', () => {
let res;

beforeAll(async () => {
res = await req.get(
`/user/${user1}?CurrentUser=${user1}&Params=${defaultParams}`,
);
});

it('Should return valid data from the database', () => {
expect(res.status).toEqual(200);
expect(res.body.username).toBe(user1);
});

it('Should return followers, saved, email', () => {
expect(res.body.followers).toBeDefined();
expect(res.body.saved).toBeDefined();
expect(res.body.email).toBeDefined();
});

it('Should not have passwords and following', () => {
expect(res.body).not.toHaveProperty('password');
expect(res.body).not.toHaveProperty('following');
});
}, 30000);

describe('Route: /user/:id -- Simulate an User Visiting Other Profile', () => {
let res;

beforeAll(async () => {
res = await req.get(
`/user/${user1}?CurrentUser=${user2}&Params=${defaultParams}`,
);
});

it('Should return valid data from the database', () => {
expect(res.status).toEqual(200);
expect(res.body.username).toBe(user1);
});

it('Should return followers', () => {
expect(res.body.followers).toBeDefined();
expect(res.body).toHaveProperty('saved');
});

it('Should not have passwords, following, email', () => {
expect(res.body).not.toHaveProperty('password');
expect(res.body).not.toHaveProperty('following');
expect(res.body).not.toHaveProperty('email');
});
}, 30000);

describe('Route: /user/:id - Error Handling', () => {
it('Should return 404 for a non-existent user', async () => {
const res = await req.get(
`/user/${unknownUser}?CurrentUser=${user1}&Params=${defaultParams}`,
);
expect(res.status).toEqual(404);
});

/*
it('Should return 400 for missing parameters', async () => {
const res = await req.get(`/user/${user1}`);
expect(res.status).toEqual(400);
});
*/

it('Should return 400 for missing CurrentUser parameter', async () => {
const res = await req.get(`/user/${user1}?Params=${defaultParams}`);
expect(res.status).toEqual(400);
});

it('Should return 200 with limited data for another user without Params', async () => {
const res = await req.get(`/user/${user2}?CurrentUser=${user1}`);
expect(res.status).toEqual(200);
expect(res.body.username).toBe(user2);
expect(res.body).not.toHaveProperty('password');
expect(res.body).not.toHaveProperty('email');
});
}, 30000);
2 changes: 1 addition & 1 deletion src/api/config/cloudinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const cloudinary = () => {
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_SECRET,
});
console.log('Cloudinary config loaded');
//console.log('Cloudinary config loaded');
return Cloudinary;
} catch (error) {
console.error('Failed to load Cloudinary config:', error);
Expand Down
Loading