1
|
|
|
import { mock, instance, when, verify, anything } from 'ts-mockito'; |
2
|
|
|
import { SchoolRepository } from 'src/Infrastructure/School/Repository/SchoolRepository'; |
3
|
|
|
import { School } from 'src/Domain/School/School.entity'; |
4
|
|
|
import { GetSchoolByIdQueryHandler } from './GetSchoolByIdQueryHandler'; |
5
|
|
|
import { GetSchoolByIdQuery } from './GetSchoolByIdQuery'; |
6
|
|
|
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException'; |
7
|
|
|
import { SchoolDetailView } from '../View/SchoolDetailView'; |
8
|
|
|
import { CanUserAccessToSchool } from 'src/Domain/User/Specification/CanUserAccessToSchool'; |
9
|
|
|
import { UserCantAccessToSchoolException } from 'src/Domain/User/Exception/UserCantAccessToSchoolException'; |
10
|
|
|
import { Status, Type } from 'src/Domain/School/AbstractSchool'; |
11
|
|
|
|
12
|
|
|
describe('GetSchoolByIdQueryHandler', () => { |
13
|
|
|
const query = new GetSchoolByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', '551d848f-a1d5-4067-9a12-5f918b69d077'); |
14
|
|
|
let schoolRepository: SchoolRepository; |
15
|
|
|
let canUserAccessToSchool: CanUserAccessToSchool; |
16
|
|
|
let queryHandler: GetSchoolByIdQueryHandler; |
17
|
|
|
let school: School; |
18
|
|
|
|
19
|
|
|
beforeEach(() => { |
20
|
|
|
school = mock(School); |
21
|
|
|
schoolRepository = mock(SchoolRepository); |
22
|
|
|
canUserAccessToSchool = mock(CanUserAccessToSchool); |
23
|
|
|
queryHandler = new GetSchoolByIdQueryHandler(instance(schoolRepository), instance(canUserAccessToSchool)); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
it('testGetSchool', async () => { |
27
|
|
|
const expectedResult = new SchoolDetailView( |
28
|
|
|
'd54f15d6-1a1d-47e8-8672-9f46018f9960', |
29
|
|
|
'Belliard', |
30
|
|
|
'LM120I', |
31
|
|
|
'127 Rue Belliard', |
32
|
|
|
'Paris', |
33
|
|
|
'75018', |
34
|
|
|
Status.PUBLIC, |
35
|
|
|
Type.ELEMENTARY, |
36
|
|
|
'[email protected]', |
37
|
|
|
'010101010101', |
38
|
|
|
10, |
39
|
|
|
200, |
40
|
|
|
'Observation' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
when(school.getName()).thenReturn('Belliard'); |
44
|
|
|
when(school.getId()).thenReturn('d54f15d6-1a1d-47e8-8672-9f46018f9960'); |
45
|
|
|
when(school.getReference()).thenReturn('LM120I'); |
46
|
|
|
when(school.getAddress()).thenReturn('127 Rue Belliard'); |
47
|
|
|
when(school.getCity()).thenReturn('Paris'); |
48
|
|
|
when(school.getZipCode()).thenReturn('75018'); |
49
|
|
|
when(school.getPhoneNumber()).thenReturn('010101010101'); |
50
|
|
|
when(school.getEmail()).thenReturn('[email protected]'); |
51
|
|
|
when(school.getNumberOfClasses()).thenReturn(10); |
52
|
|
|
when(school.getNumberOfStudents()).thenReturn(200); |
53
|
|
|
when(school.getObservation()).thenReturn('Observation'); |
54
|
|
|
when(school.getType()).thenReturn(Type.ELEMENTARY); |
55
|
|
|
when(school.getStatus()).thenReturn(Status.PUBLIC); |
56
|
|
|
when( |
57
|
|
|
schoolRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
58
|
|
|
).thenResolve(instance(school)); |
59
|
|
|
when( |
60
|
|
|
canUserAccessToSchool.isSatisfiedBy(instance(school), '551d848f-a1d5-4067-9a12-5f918b69d077') |
61
|
|
|
).thenResolve(true); |
62
|
|
|
expect(await queryHandler.execute(query)).toMatchObject(expectedResult); |
63
|
|
|
|
64
|
|
|
verify( |
65
|
|
|
schoolRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
66
|
|
|
).once(); |
67
|
|
|
verify( |
68
|
|
|
canUserAccessToSchool.isSatisfiedBy(instance(school), '551d848f-a1d5-4067-9a12-5f918b69d077') |
69
|
|
|
).once(); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
it('testGetSchoolNotFound', async () => { |
73
|
|
|
when( |
74
|
|
|
schoolRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
75
|
|
|
).thenResolve(null); |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
expect(await queryHandler.execute(query)).toBeUndefined(); |
79
|
|
|
} catch (e) { |
80
|
|
|
expect(e).toBeInstanceOf(SchoolNotFoundException); |
81
|
|
|
expect(e.message).toBe('schools.errors.not_found'); |
82
|
|
|
verify( |
83
|
|
|
schoolRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
84
|
|
|
).once(); |
85
|
|
|
verify(canUserAccessToSchool.isSatisfiedBy(anything(), anything())).never(); |
86
|
|
|
} |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
it('testUserCantAccessToSchool', async () => { |
90
|
|
|
when( |
91
|
|
|
schoolRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
92
|
|
|
).thenResolve(instance(school)); |
93
|
|
|
when( |
94
|
|
|
canUserAccessToSchool.isSatisfiedBy(instance(school), '551d848f-a1d5-4067-9a12-5f918b69d077') |
95
|
|
|
).thenResolve(false); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
expect(await queryHandler.execute(query)).toBeUndefined(); |
99
|
|
|
} catch (e) { |
100
|
|
|
expect(e).toBeInstanceOf(UserCantAccessToSchoolException); |
101
|
|
|
expect(e.message).toBe('users.errors.cant_access_to_school'); |
102
|
|
|
verify( |
103
|
|
|
schoolRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
104
|
|
|
).once(); |
105
|
|
|
verify( |
106
|
|
|
canUserAccessToSchool.isSatisfiedBy(instance(school), '551d848f-a1d5-4067-9a12-5f918b69d077') |
107
|
|
|
).once(); |
108
|
|
|
} |
109
|
|
|
}); |
110
|
|
|
}); |
111
|
|
|
|