Skip to content

Commit

Permalink
added contributions page
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoodalisha committed Nov 23, 2024
2 parents c14dc1b + eecff6e commit 9a3338d
Show file tree
Hide file tree
Showing 32 changed files with 2,607 additions and 519 deletions.
35 changes: 22 additions & 13 deletions .github/workflows/frontend-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:
branches: [feature/webiu-2024]

jobs:
build-and-deploy:
name: Build, Test, and Deploy Angular (webiu-ui)
build-webiu-ui:
name: Build and Test Angular (webiu-ui)
runs-on: ubuntu-latest

steps:
Expand All @@ -20,28 +20,37 @@ jobs:
with:
node-version: '20'

- name: Checkout target branch
uses: actions/checkout@v4
with:
ref: feature/webiu-2024

- name: Fetch pull request changes
run: |
git fetch origin +refs/pull/${{ github.event.pull_request.number }}/merge:pr
git checkout pr
- name: Install Angular CLI
run: npm install -g @angular/cli
run: |
cd webiu-ui
npm install -g @angular/cli
- name: Install dependencies
run: |
cd webiu-ui
npm install
- name: Build Angular App
- name: Install Chrome
run: |
cd webiu-ui
ng build --output-path=../docs --base-href="/Webiu/"
sudo apt-get update
sudo apt-get install -y google-chrome-stable
- name: Run Angular Tests
env:
CHROME_BIN: google-chrome
- name: Build Angular App
run: |
cd webiu-ui
ng test --watch=false --browsers=ChromeHeadless --no-sandbox
npm run build
- name: Deploy to GitHub Pages
if: github.ref == 'refs/heads/feature/webiu-2024'
- name: Run Angular Tests
run: |
cd webiu-ui
ngh --dir=../docs
npm test -- --browsers=ChromeHeadless --watch=false
5 changes: 5 additions & 0 deletions webiu-server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PORT=5100
MONGODB_URI=mongodb://localhost:27017/webiu
JWT_SECRET=your_jwt_secret
GITHUB_ACCESS_TOKEN=your_github_access_token_add_here

176 changes: 176 additions & 0 deletions webiu-server/__tests__/authController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
const request = require('supertest');
const express = require('express');
const User = require('../models/User');
const { signToken } = require('../utils/jwt');
const authController = require('../controllers/authController');

jest.mock('../utils/jwt', () => ({
signToken: jest.fn().mockReturnValue('mockedToken'),
}));

const app = express();
app.use(express.json());
app.post('/register', authController.register);
app.post('/login', authController.login);

describe('Auth Controller Tests', () => {
beforeEach(() => {
jest.clearAllMocks();
});

// Test successful user registration
it('should register a user successfully', async () => {
const mockUser = {
_id: '60d6f96a9b1f8f001c8f27c5',
name: 'John Doe',
email: '[email protected]',
password: 'password123',
};

User.findOne = jest.fn().mockResolvedValue(null);
User.prototype.save = jest.fn().mockResolvedValue(mockUser);

const response = await request(app).post('/register').send({
name: 'John Doe',
email: '[email protected]',
password: 'password123',
confirmPassword: 'password123',
});

response.body.data.user.id = mockUser._id;

expect(response.status).toBe(201);
expect(response.body.status).toBe('success');
expect(response.body.data.user).toEqual({
id: mockUser._id,
name: mockUser.name,
email: mockUser.email,
});
expect(response.body.data.token).toBe('mockedToken');
});

// Test failed user registration - email already exists
it('should return an error when email already exists during registration', async () => {
const mockUser = {
_id: '60d6f96a9b1f8f001c8f27c5',
name: 'John Doe',
email: '[email protected]',
password: 'password123',
};

User.findOne = jest.fn().mockResolvedValue(mockUser);

const response = await request(app).post('/register').send({
name: 'John Doe',
email: '[email protected]',
password: 'password123',
confirmPassword: 'password123',
});

expect(response.status).toBe(400);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('User already exists');
});

// Test failed user registration - password mismatch
it('should return an error when passwords do not match', async () => {
const response = await request(app).post('/register').send({
name: 'John Doe',
email: '[email protected]',
password: 'password123',
confirmPassword: 'password456',
});

expect(response.status).toBe(400);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('Passwords do not match');
});

// Test successful user login
it('should login a user successfully', async () => {
const mockUser = {
_id: '60d6f96a9b1f8f001c8f27c5',
email: '[email protected]',
password: 'password123',
matchPassword: jest.fn().mockResolvedValue(true),
githubId: 'john-github',
};

User.findOne = jest.fn().mockResolvedValue(mockUser);

const response = await request(app).post('/login').send({
email: '[email protected]',
password: 'password123',
});

expect(response.status).toBe(200);
expect(response.body.status).toBe('success');
expect(response.body.data.user).toEqual({
id: mockUser._id,
name: mockUser.name,
email: mockUser.email,
githubId: mockUser.githubId,
});
expect(response.body.data.token).toBe('mockedToken');
});

// Test failed user login - incorrect password
it('should return an error for incorrect password during login', async () => {
const mockUser = {
_id: '60d6f96a9b1f8f001c8f27c5',
email: '[email protected]',
password: 'password123',
matchPassword: jest.fn().mockResolvedValue(false),
};

User.findOne = jest.fn().mockResolvedValue(mockUser);

const response = await request(app).post('/login').send({
email: '[email protected]',
password: 'wrongPassword',
});

expect(response.status).toBe(401);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('Invalid email or password');
});

// Test failed user login - user not found
it('should return an error if user is not found during login', async () => {
User.findOne = jest.fn().mockResolvedValue(null);

const response = await request(app).post('/login').send({
email: '[email protected]',
password: 'password123',
});

expect(response.status).toBe(401);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('User not found');
});

// Test failed user registration - invalid email format
it('should return an error for invalid email format during registration', async () => {
const response = await request(app).post('/register').send({
name: 'John Doe',
email: 'invalid-email',
password: 'password123',
confirmPassword: 'password123',
});

expect(response.status).toBe(400);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('Invalid email format');
});

// Test failed user login - missing fields
it('should return an error if required fields are missing during login', async () => {
const response = await request(app).post('/login').send({
email: '[email protected]',
});

expect(response.status).toBe(401);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('User not found');
});
});
13 changes: 12 additions & 1 deletion webiu-server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ const cookieParser = require('cookie-parser');
const cors = require('cors');
const contributorRoutes = require('./routes/contributorRoutes');
const projectRoutes = require('./routes/projectRoutes');
const authRoutes = require('./routes/authRoutes')
const connectDB = require('./config/db');


const app = express();
connectDB();

app.use(cors());
app.use(express.json());
app.use(cookieParser());

app.use('/api/v1/project', projectRoutes);
// Route for API-testing
app.get('/api/v1/test', (req, res) => {
res.status(200).json({ message: 'Server is running and working fine!' });
});

app.use('/api/v1/project', projectRoutes);
app.use('/api/contributor',contributorRoutes);
app.use('/api/v1/auth',authRoutes)



module.exports = app;
16 changes: 16 additions & 0 deletions webiu-server/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose');
require('dotenv').config();
const colors = require('colors');


const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI);
console.log('MongoDB connection established successfully.'.green.bold.underline);
} catch (error) {
console.error('Error: MongoDB connection failed. Please check the database server and configuration.'.red.bold.underline);
process.exit(1);
}
};

module.exports = connectDB;
103 changes: 103 additions & 0 deletions webiu-server/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// controllers/authController.js
const User = require('../models/User');
const { signToken } = require('../utils/jwt');

const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;


const register = async (req, res) => {
const { name, email, password, confirmPassword, githubId } = req.body;

if (!emailRegex.test(email)) {
return res.status(400).json({
status: 'error',
message: 'Invalid email format',
});
}

if (password !== confirmPassword) {
return res.status(400).json({
status: 'error',
message: 'Passwords do not match',
});
}

try {
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({
status: 'error',
message: 'User already exists',
});
}

const user = new User({ name, email, password, githubId });
await user.save();

const token = signToken(user);

res.status(201).json({
status: 'success',
message: 'User registered successfully',
data: {
user: {
id: user._id,
name: user.name,
email: user.email,
},
token,
},
});
} catch (error) {
res.status(500).json({
status: 'error',
message: error.message,
});
}
};


const login = async (req, res) => {
const { email, password } = req.body;

try {
const user = await User.findOne({ email });

if(!user){
return res.status(401).json({
status: 'error',
message: 'User not found',
});
}
if (!user || !(await user.matchPassword(password))) {
return res.status(401).json({
status: 'error',
message: 'Invalid email or password',
});
}


const token = signToken(user);

res.status(200).json({
status: 'success',
message: 'Login successful',
data: {
user: {
id: user._id,
name: user.name,
email: user.email,
githubId:user.githubId
},
token,
},
});
} catch (error) {
res.status(500).json({
status: 'error',
message: error.message,
});
}
};

module.exports = { register, login };
Loading

0 comments on commit 9a3338d

Please sign in to comment.