-
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.
[GH-38] feat(User): add MongoAuthRepository
- Loading branch information
1 parent
93060ad
commit f4357b6
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/Contexts/apiApp/Auth/infrastructure/persistence/MongoAuthRepository.ts
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,57 @@ | ||
import { MongoClient } from 'mongodb'; | ||
import { Nullable } from '../../../../shared/domain/Nullable'; | ||
import { MongoRepository } from '../../../../shared/infrastructure/persistence/mongo/MongoRepository'; | ||
import { User, UserRepository } from '../../domain'; | ||
import { UserPatch } from '../../domain/UserPatch'; | ||
|
||
export interface AuthDocument { | ||
_id: string; | ||
email: string; | ||
username: string; | ||
password: string; | ||
emailValidated: boolean; | ||
roles: string[]; | ||
} | ||
|
||
export class MongoAuthRepository | ||
extends MongoRepository<User | UserPatch> | ||
implements UserRepository | ||
{ | ||
constructor(client: Promise<MongoClient>) { | ||
super(client); | ||
this.createUniqueIndex(); | ||
} | ||
protected collectionName(): string { | ||
return 'users'; | ||
} | ||
|
||
async save(user: User): Promise<void> { | ||
return this.persist(user.id.value, user); | ||
} | ||
|
||
async update(user: UserPatch): Promise<void> { | ||
return this.patch(user); | ||
} | ||
|
||
async search(email: string): Promise<Nullable<User>> { | ||
const collection = await this.collection(); | ||
const document = await collection.findOne<AuthDocument>({ email }); | ||
|
||
return document | ||
? User.fromPrimitives({ | ||
id: document._id, | ||
email: document.email, | ||
username: document.username, | ||
password: document.password, | ||
emailValidated: document.emailValidated, | ||
roles: document.roles | ||
}) | ||
: null; | ||
} | ||
|
||
private async createUniqueIndex(): Promise<void> { | ||
const collection = await this.collection(); | ||
await collection.createIndex({ email: 1 }, { unique: true }); | ||
await collection.createIndex({ username: 1 }, { unique: true }); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/apps/apiApp/dependency-injection/Auth/application.yaml
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,31 @@ | ||
services: | ||
apiApp.MongoConfig: | ||
factory: | ||
class: ../../../../Contexts/shared/infrastructure/persistence/mongo/MongoConfigFactory | ||
method: 'createConfig' | ||
|
||
apiApp.MongoClient: | ||
factory: | ||
class: ../../../../Contexts/shared/infrastructure/persistence/mongo/MongoClientFactory | ||
method: 'createClient' | ||
arguments: ['apiApp', '@apiApp.MongoConfig'] | ||
|
||
# plugin.Encrypter: | ||
# class: ../../../../Contexts/shared/plugins/CryptAdapter | ||
# arguments: [] | ||
|
||
apiApp.Auth.domain.AuthRepository: | ||
class: ../../../../Contexts/apiApp/Auth/infrastructure/persistence/MongoAuthRepository | ||
arguments: ['@apiApp.MongoClient'] | ||
|
||
# apiApp.Auth.application.LoginUser: | ||
# class: ../../../../Contexts/apiApp/Auth/application/LoginUser | ||
# arguments: ['@apiApp.Auth.domain.AuthRepository', '@plugin.Encrypter'] | ||
|
||
# apiApp.Auth.application.RegisterUser: | ||
# class: ../../../../Contexts/apiApp/Auth/application/RegisterUser | ||
# arguments: ['@apiApp.Auth.domain.AuthRepository', '@plugin.Encrypter'] | ||
|
||
# apiApp.Auth.application.ValidateMail: | ||
# class: ../../../../Contexts/apiApp/Auth/application/ValidateMail | ||
# arguments: ['@apiApp.Auth.domain.AuthRepository', '@plugin.Encrypter'] |
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,3 +1,4 @@ | ||
imports: | ||
- { resource: ./apps/application.yaml } | ||
- { resource: ./Auth/application.yaml } | ||
- { resource: ./Books/application.yaml } |
62 changes: 62 additions & 0 deletions
62
tests/Contexts/apiApp/Auth/infrastructure/persistence/MongoAuthRepository.test.ts
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,62 @@ | ||
import container from '../../../../../../src/apps/apiApp/dependency-injection'; | ||
import { UserRepository } from '../../../../../../src/Contexts/apiApp/Auth/domain/UserRepository'; | ||
|
||
import { EnvironmentArranger } from '../../../../shared/infrastructure/arranger/EnvironmentArranger'; | ||
import { UserMother } from '../../domain/mothers'; | ||
|
||
const repository: UserRepository = container.get( | ||
'apiApp.Auth.domain.AuthRepository' | ||
); | ||
|
||
const environmentArranger: Promise<EnvironmentArranger> = container.get( | ||
'apiApp.EnvironmentArranger' | ||
); | ||
|
||
describe('MongoAuthRepository', () => { | ||
beforeEach(async () => { | ||
await (await environmentArranger).arrange(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await (await environmentArranger).arrange(); | ||
await (await environmentArranger).close(); | ||
}); | ||
|
||
describe('save', () => { | ||
it('should save a user', async () => { | ||
const user = UserMother.random(); | ||
|
||
await repository.save(user); | ||
}); | ||
}); | ||
|
||
describe('update', () => { | ||
it('should update an existing user', async () => { | ||
const user = UserMother.random(); | ||
await repository.save(user); | ||
const userPatch = UserMother.randomPatch(user.id.value); | ||
|
||
await repository.update(userPatch); | ||
|
||
expect(await repository.search(user.email.value)).toMatchObject( | ||
userPatch | ||
); | ||
}); | ||
}); | ||
|
||
describe('search', () => { | ||
it('should return an existing user', async () => { | ||
const user = UserMother.random(); | ||
|
||
await repository.save(user); | ||
|
||
expect(await repository.search(user.email.value)).toMatchObject(user); | ||
}); | ||
|
||
it('should not return a non existing user', async () => { | ||
expect(await repository.search(UserMother.random().email.value)).toBe( | ||
null | ||
); | ||
}); | ||
}); | ||
}); |