-
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.
added routes for authenticate domain & test logic (#57)
Co-authored-by: Tejas Mehta <[email protected]>
- Loading branch information
1 parent
b47ff29
commit 25cdb55
Showing
21 changed files
with
1,145 additions
and
852 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
Injectable, | ||
NestMiddleware, | ||
Inject, | ||
OnModuleInit, | ||
HttpException, | ||
HttpStatus, | ||
} from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { ClientGrpc } from '@nestjs/microservices'; | ||
import { JwtProto } from 'juno-proto'; | ||
import { lastValueFrom } from 'rxjs'; | ||
|
||
const { JWT_SERVICE_NAME } = JwtProto; | ||
|
||
@Injectable() | ||
export class EmailLinkingMiddleware implements NestMiddleware, OnModuleInit { | ||
private jwtService: JwtProto.JwtServiceClient; | ||
|
||
constructor(@Inject(JWT_SERVICE_NAME) private jwtClient: ClientGrpc) {} | ||
|
||
onModuleInit() { | ||
this.jwtService = this.jwtClient.getService<JwtProto.JwtServiceClient>( | ||
JwtProto.JWT_SERVICE_NAME, | ||
); | ||
} | ||
|
||
async use(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
if (!req.headers.authorization) { | ||
throw new Error('No authorization headers'); | ||
} | ||
const token = this.extractTokenFromHeader(req); | ||
if (!token) { | ||
throw new Error('Jwt not found'); | ||
} | ||
const jwtValidation = this.jwtService.validateJwt({ jwt: token }); | ||
await lastValueFrom(jwtValidation); | ||
next(); | ||
} catch { | ||
throw new HttpException( | ||
'Invalid user credentials', | ||
HttpStatus.UNAUTHORIZED, | ||
); | ||
} | ||
} | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const [type, token] = request.headers.authorization?.split(' ') ?? []; | ||
return type === 'Bearer' ? token : undefined; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -53,14 +53,14 @@ describe('Email Registration Routes', () => { | |
it('Registers an email without a body', () => { | ||
const token = jwt.sign({}, 'secret'); | ||
return request(app.getHttpServer()) | ||
.post('/email/register') | ||
.post('/email/register-sender') | ||
.set('Authorization', 'Bearer ' + token) | ||
.expect(400); | ||
}); | ||
it('Has been called with a malformed emaiil', () => { | ||
const token = jwt.sign({}, 'secret'); | ||
return request(app.getHttpServer()) | ||
.post('/email/register') | ||
.post('/email/register-sender') | ||
.set('Authorization', 'Bearer ' + token) | ||
.send({ | ||
email: 'invalidemail', // Malformed email | ||
|
@@ -69,15 +69,15 @@ describe('Email Registration Routes', () => { | |
}); | ||
it('Registration endpoint called with no Authorization header', () => { | ||
return request(app.getHttpServer()) | ||
.post('/email/register') | ||
.post('/email/register-sender') | ||
.send({ | ||
email: '[email protected]', | ||
}) | ||
.expect(401); | ||
}); | ||
it('Registration endpoint called with an invalid JWT', () => { | ||
return request(app.getHttpServer()) | ||
.post('/email/register') | ||
.post('/email/register-sender') | ||
.set('Authorization', 'Bearer invalid.jwt.token') | ||
.send({ | ||
email: '[email protected]', | ||
|
@@ -88,7 +88,7 @@ describe('Email Registration Routes', () => { | |
// Assuming 'valid.jwt.token' is a placeholder for a valid JWT obtained in a way relevant to your test setup | ||
const token = jwt.sign({}, 'secret'); | ||
return request(app.getHttpServer()) | ||
.post('/email/register') | ||
.post('/email/register-sender') | ||
.set('Authorization', 'Bearer ' + token) | ||
.send({ | ||
email: '[email protected]', | ||
|
@@ -98,21 +98,6 @@ describe('Email Registration Routes', () => { | |
}); | ||
|
||
describe('Email Sending Route', () => { | ||
let app: INestApplication; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
it('should return 401 when Authorization header is missing', async () => { | ||
return request(app.getHttpServer()) | ||
.post('/email/send') | ||
|
@@ -398,3 +383,46 @@ describe('Email Sending Route', () => { | |
.expect(400); | ||
}); | ||
}); | ||
|
||
describe('Domain Registration Routes', () => { | ||
it('Registers a domain without a domain parameter', () => { | ||
const token = jwt.sign({}, 'secret'); | ||
return request(app.getHttpServer()) | ||
.post('/email/register-domain') | ||
.set('Authorization', 'Bearer ' + token) | ||
.expect(400); | ||
}); | ||
|
||
it('Registers a domain with valid parameters', () => { | ||
const token = jwt.sign({}, 'secret'); | ||
return request(app.getHttpServer()) | ||
.post('/email/register-domain') | ||
.set('Authorization', 'Bearer ' + token) | ||
.send({ | ||
domain: 'example.com', | ||
subdomain: 'sub', | ||
}) | ||
.expect(201); | ||
}); | ||
|
||
it('Registration endpoint called with no Authorization header', () => { | ||
return request(app.getHttpServer()) | ||
.post('/email/register-domain') | ||
.send({ | ||
domain: 'example.com', | ||
subdomain: 'sub', | ||
}) | ||
.expect(401); | ||
}); | ||
|
||
it('Registration endpoint called with an invalid JWT', () => { | ||
return request(app.getHttpServer()) | ||
.post('/email/register-domain') | ||
.set('Authorization', 'Bearer invalid.jwt.token') | ||
.send({ | ||
domain: 'example.com', | ||
subdomain: 'sub', | ||
}) | ||
.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
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,4 +1,3 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService {} |
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
Oops, something went wrong.