Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ add mutation signIn of AuthResolver #82

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions libs/auth/application/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# auth-application

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test auth-application` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/auth/application/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('../../../eslint.config.js');

module.exports = [...baseConfig];
10 changes: 10 additions & 0 deletions libs/auth/application/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'auth-application',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../coverage/libs/auth/application',
};
9 changes: 9 additions & 0 deletions libs/auth/application/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "auth-application",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/auth/application/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project auth-application --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/auth/application/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/auth.service';
27 changes: 27 additions & 0 deletions libs/auth/application/src/lib/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';

describe('AuthService', () => {
let service: AuthService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile();

service = module.get<AuthService>(AuthService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('signIn', () => {
it('should return an access token', async () => {
const result = await service.signIn('[email protected]', 'password123');

expect(result).toHaveProperty('accessToken');
expect(typeof result.accessToken).toBe('string');
});
});
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
});
17 changes: 17 additions & 0 deletions libs/auth/application/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class AuthService {

async signIn(
username: string,
pass: string,
): Promise<{
accessToken: string;
}> {
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Implement sign in
// Step 1: Validate user credentials via AWS Cognito
// Step 2: Retrieve user from the database
// Step 3: Generate a custom JWT access token
return {
accessToken: 'accessToken',
};
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
}
}
16 changes: 16 additions & 0 deletions libs/auth/application/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/auth/application/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
14 changes: 14 additions & 0 deletions libs/auth/application/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
7 changes: 7 additions & 0 deletions libs/auth/interface-adapters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# auth-interface-adapters

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test auth-interface-adapters` to execute the unit tests via [Jest](https://jestjs.io).
3 changes: 3 additions & 0 deletions libs/auth/interface-adapters/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('../../../eslint.config.js');

module.exports = [...baseConfig];
10 changes: 10 additions & 0 deletions libs/auth/interface-adapters/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'auth-interface-adapters',
preset: '../../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../coverage/libs/auth/interface-adapters',
};
9 changes: 9 additions & 0 deletions libs/auth/interface-adapters/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "auth-interface-adapters",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/auth/interface-adapters/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project auth-interface-adapters --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/auth/interface-adapters/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/auth.module';
10 changes: 10 additions & 0 deletions libs/auth/interface-adapters/src/lib/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AuthService } from '@auth/application';
import { Module } from '@nestjs/common';

import { AuthResolver } from './resolver/auth.resolver';

@Module({
providers: [AuthResolver, AuthService],
exports: [],
})
export class AuthModule {}
9 changes: 9 additions & 0 deletions libs/auth/interface-adapters/src/lib/dto/sign-in-input.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Field } from '@nestjs/graphql';

export class SignInInputDto {
@Field()
email!: string;
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved

@Field()
password!: string;
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from '@auth/application';
import { AuthResolver } from './auth.resolver';
import { SignInInputDto } from '../dto/sign-in-input.dto';

describe('AuthResolver', () => {
let resolver: AuthResolver;
let authService: AuthService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
AuthResolver,
{
provide: AuthService,
useValue: {
signIn: jest.fn(),
},
},
],
}).compile();

resolver = module.get<AuthResolver>(AuthResolver);
authService = module.get<AuthService>(AuthService);
});

it('should be defined', () => {
expect(resolver).toBeDefined();
});

describe('signIn', () => {
it('should call authService.signIn with correct parameters', async () => {
const signInInput: SignInInputDto = {
email: '[email protected]',
password: 'password123',
};
const expectedResult = { accessToken: 'testToken' };

jest.spyOn(authService, 'signIn').mockResolvedValue(expectedResult);

const result = await resolver.signIn(signInInput);

expect(authService.signIn).toHaveBeenCalledWith(
signInInput.email,
signInInput.password
);
expect(result).toEqual(expectedResult);
});
});
});
19 changes: 19 additions & 0 deletions libs/auth/interface-adapters/src/lib/resolver/auth.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AuthService } from '@auth/application';
import { Resolver, Mutation, Args } from '@nestjs/graphql';

import { SignInInputDto } from '../dto/sign-in-input.dto';

@Resolver()
export class AuthResolver {
constructor(private authService: AuthService) {}

@Mutation()
async signIn(
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
@Args({ name: 'signInInput', type: () => SignInInputDto })
signInInput: SignInInputDto,
): Promise<{
accessToken: string;
}> {
return this.authService.signIn(signInInput.email, signInInput.password);
}
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
}
22 changes: 22 additions & 0 deletions libs/auth/interface-adapters/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
16 changes: 16 additions & 0 deletions libs/auth/interface-adapters/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node"],
"target": "es2021",
"strictNullChecks": true,
"noImplicitAny": true,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
14 changes: 14 additions & 0 deletions libs/auth/interface-adapters/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
6 changes: 4 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"aws-cognito": ["libs/shared/infrastructure/aws-cognito/src/index.ts"],
"@auth/application": ["libs/auth/application/src/index.ts"],
"@auth/interface-adapters": ["libs/auth/interface-adapters/src/index.ts"],
"@prompt/domain": ["libs/prompt/domain/src/index.ts"],
"@shared/config": ["libs/shared/config/src/index.ts"],
"@shared/infrastructure-mongoose": [
Expand All @@ -28,7 +29,8 @@
],
"@users/interface-adapters": [
"libs/users/interface-adapters/src/index.ts"
]
],
"aws-cognito": ["libs/shared/infrastructure/aws-cognito/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down