-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: Add auth, user module E2E test code and add coverage test
- Loading branch information
Showing
10 changed files
with
353 additions
and
56 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
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,37 @@ | ||
import { INestApplication, LoggerService } from '@nestjs/common'; | ||
import { PrismaService } from './prisma/prisma.service'; | ||
import { SystemLoggerService } from './system-logger/system-logger.service'; | ||
import * as bcrypt from 'bcryptjs'; | ||
|
||
export async function InitializeAdmin(app: INestApplication) { | ||
// Generate default admin account | ||
const prisma = app.get<PrismaService>(PrismaService); | ||
const logger = app.get<LoggerService>(SystemLoggerService); | ||
|
||
if (!(process.env.ADMIN_EMAIL && process.env.ADMIN_PW)) { | ||
logger.error( | ||
'Admin Email and Admin Pw not found. Please configure `.env` file and reboot', | ||
); | ||
throw new Error('Fail to initialize server'); | ||
} else { | ||
const findAdmin = await prisma.user.findUnique({ | ||
where: { | ||
email: process.env.ADMIN_EMAIL, | ||
}, | ||
}); | ||
if (!findAdmin) { | ||
// Initialize root admin | ||
await prisma.user.create({ | ||
data: { | ||
nickname: 'admin', | ||
password: bcrypt.hashSync(process.env.ADMIN_PW, 10), | ||
email: process.env.ADMIN_EMAIL, | ||
type: 'Admin', | ||
}, | ||
}); | ||
logger.log('Admin initialized'); | ||
} else { | ||
logger.log('Admin already initialized'); | ||
} | ||
} | ||
} |
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,65 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AppModule } from 'app/app.module'; | ||
import * as request from 'supertest'; | ||
import { userSignupGen } from 'test/mock-generator'; | ||
|
||
describe('/auth Auth Controller', () => { | ||
let app: INestApplication; | ||
|
||
// Mock user | ||
const user1 = userSignupGen(); | ||
|
||
beforeAll(async () => { | ||
const testModule: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
app = testModule.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
// Signup test | ||
describe('/singup POST', () => { | ||
it('should sign up', () => { | ||
return request(app.getHttpServer()) | ||
.post('/auth/signup') | ||
.send(user1) | ||
.expect(200); | ||
}); | ||
|
||
it('should throw if credential already taken', () => { | ||
return request(app.getHttpServer()) | ||
.post('/auth/signup') | ||
.send(user1) | ||
.expect(400); | ||
}); | ||
}); | ||
|
||
//Signin test | ||
describe('/signin POST', () => { | ||
it('should singin', () => { | ||
return request(app.getHttpServer()) | ||
.post('/auth/signin') | ||
.send({ | ||
password: user1.password, | ||
email: user1.email, | ||
}) | ||
.expect(200); | ||
}); | ||
|
||
it('should throw if credential is not correct', () => { | ||
return request(app.getHttpServer()) | ||
.post('/auth/signin') | ||
.send({ | ||
email: user1.email, | ||
password: 'wrong-password', | ||
}) | ||
.expect(401); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.
9e0e660
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blue/Green Deployment
/user/admin/role
->/user/role
/judge/:pid/submit
->/judge/:pid/submission