2
2
3
3
namespace ApiSkeletonsTest \Laravel \Doctrine \ApiKey \Feature \Repository ;
4
4
5
+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \AdminEvent ;
5
6
use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \ApiKey ;
6
7
use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \Scope ;
7
8
use ApiSkeletons \Laravel \Doctrine \ApiKey \Exception \ApiKeyDoesNotHaveScope ;
@@ -25,6 +26,14 @@ public function testGenerate(): void
25
26
$ this ->assertEquals ('testing ' , $ apiKey ->getName ());
26
27
$ this ->assertEquals (64 , strlen ($ apiKey ->getApiKey ()));
27
28
$ this ->assertEquals (true , $ apiKey ->getIsActive ());
29
+
30
+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
31
+ ->findAll ();
32
+
33
+ foreach ($ adminEvents as $ adminEvent ) {
34
+ $ this ->assertEquals ('generate ' , $ adminEvent ->getEvent ());
35
+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
36
+ }
28
37
}
29
38
30
39
public function testGenerateValidatesName (): void
@@ -50,7 +59,7 @@ public function testGenerateDoesNotCollideNames(): void
50
59
$ result = $ repository ->generate ('testing ' );
51
60
}
52
61
53
- public function testSetStatus (): void
62
+ public function testDeativate (): void
54
63
{
55
64
$ entityManager = $ this ->createDatabase (app ('em ' ));
56
65
$ repository = $ entityManager ->getRepository (ApiKey::class);
@@ -66,6 +75,49 @@ public function testSetStatus(): void
66
75
67
76
$ this ->assertGreaterThan ($ beforeSetStatus , $ apiKey ->getStatusAt ());
68
77
$ this ->assertEquals (false , $ apiKey ->getIsActive ());
78
+
79
+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
80
+ ->findBy ([
81
+ 'apiKey ' => $ apiKey ,
82
+ 'event ' => 'deactivate ' ,
83
+ ]);
84
+
85
+ $ this ->assertEquals (1 , count ($ adminEvents ));
86
+ foreach ($ adminEvents as $ adminEvent ) {
87
+ $ this ->assertEquals ('deactivate ' , $ adminEvent ->getEvent ());
88
+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
89
+ }
90
+ }
91
+
92
+ public function testActivate (): void
93
+ {
94
+ $ entityManager = $ this ->createDatabase (app ('em ' ));
95
+ $ repository = $ entityManager ->getRepository (ApiKey::class);
96
+ $ apiKey = $ repository ->generate ('testing ' );
97
+ $ apiKey ->setIsActive (false );
98
+ $ entityManager ->flush ();
99
+
100
+ $ beforeSetStatus = new DateTime ();
101
+
102
+ $ this ->assertEquals (false , $ apiKey ->getIsActive ());
103
+
104
+ $ repository ->updateActive ($ apiKey , true );
105
+ $ entityManager ->flush ();
106
+
107
+ $ this ->assertGreaterThan ($ beforeSetStatus , $ apiKey ->getStatusAt ());
108
+ $ this ->assertEquals (true , $ apiKey ->getIsActive ());
109
+
110
+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
111
+ ->findBy ([
112
+ 'apiKey ' => $ apiKey ,
113
+ 'event ' => 'activate ' ,
114
+ ]);
115
+
116
+ $ this ->assertEquals (1 , count ($ adminEvents ));
117
+ foreach ($ adminEvents as $ adminEvent ) {
118
+ $ this ->assertEquals ('activate ' , $ adminEvent ->getEvent ());
119
+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
120
+ }
69
121
}
70
122
71
123
public function testAddScope (): void
@@ -91,6 +143,18 @@ public function testAddScope(): void
91
143
}
92
144
93
145
$ this ->assertTrue ($ found );
146
+
147
+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
148
+ ->findBy ([
149
+ 'apiKey ' => $ apiKey ,
150
+ 'event ' => 'add scope: ' . $ scope ->getName (),
151
+ ]);
152
+
153
+ $ this ->assertEquals (1 , count ($ adminEvents ));
154
+ foreach ($ adminEvents as $ adminEvent ) {
155
+ $ this ->assertEquals ('add scope: ' . $ scope ->getName (), $ adminEvent ->getEvent ());
156
+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
157
+ }
94
158
}
95
159
96
160
public function testCannotAddSameScopeTwice (): void
@@ -131,6 +195,18 @@ public function testRemoveScope(): void
131
195
$ entityManager ->flush ();
132
196
133
197
$ this ->assertEquals (0 , sizeof ($ apiKey ->getScopes ()));
198
+
199
+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
200
+ ->findBy ([
201
+ 'apiKey ' => $ apiKey ,
202
+ 'event ' => 'remove scope: ' . $ scope ->getName (),
203
+ ]);
204
+
205
+ $ this ->assertEquals (1 , count ($ adminEvents ));
206
+ foreach ($ adminEvents as $ adminEvent ) {
207
+ $ this ->assertEquals ('remove scope: ' . $ scope ->getName (), $ adminEvent ->getEvent ());
208
+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
209
+ }
134
210
}
135
211
136
212
public function testRemoveScopeWhichIsNotAssigned (): void
0 commit comments