forked from OpenBankProject/OBP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogCacheEndpointTest.scala
More file actions
247 lines (197 loc) · 11.5 KB
/
LogCacheEndpointTest.scala
File metadata and controls
247 lines (197 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package code.api.v5_1_0
import code.api.util.APIUtil.OAuth._
import code.api.util.ApiRole.{CanGetSystemLogCacheAll,CanGetSystemLogCacheInfo}
import code.api.util.ErrorMessages.{UserHasMissingRoles, UserNotLoggedIn}
import code.api.v5_1_0.OBPAPI5_1_0.Implementations5_1_0
import code.entitlement.Entitlement
import com.github.dwickern.macros.NameOf.nameOf
import com.openbankproject.commons.model.ErrorMessage
import com.openbankproject.commons.util.ApiVersion
import net.liftweb.json.JsonAST._
import org.scalatest.Tag
class LogCacheEndpointTest extends V510ServerSetup {
/**
* Test tags
* Example: To run tests with tag "logCacheEndpoint":
* mvn test -D tagsToInclude
*
* This is made possible by the scalatest maven plugin
*/
object VersionOfApi extends Tag(ApiVersion.v5_1_0.toString)
object ApiEndpoint1 extends Tag(nameOf(Implementations5_1_0.logCacheInfoEndpoint))
feature(s"test $ApiEndpoint1 version $VersionOfApi - Unauthorized access") {
scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) {
When("We make a request v5.1.0")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET
val response = makeGetRequest(request)
Then("We should get a 401")
response.code should equal(401)
response.body.extract[ErrorMessage].message should equal(UserNotLoggedIn)
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Missing entitlement") {
scenario("We will call the endpoint with user credentials but without proper entitlement", ApiEndpoint1, VersionOfApi) {
When("We make a request v5.1.0")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1)
val response = makeGetRequest(request)
Then("error should be " + UserHasMissingRoles + CanGetSystemLogCacheAll)
response.code should equal(403)
response.body.extract[ErrorMessage].message contains (UserHasMissingRoles) shouldBe (true)
response.body.extract[ErrorMessage].message contains CanGetSystemLogCacheInfo.toString() shouldBe (true)
response.body.extract[ErrorMessage].message contains CanGetSystemLogCacheAll.toString() shouldBe (true)
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Authorized access without pagination") {
scenario("We get log cache without pagination parameters", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request to get log cache")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1)
val response = makeGetRequest(request)
Then("We should get a successful response")
response.code should equal(200)
val json = response.body.extract[JObject]
And("The response should contain log entries")
(json \ "entries") should not be JNothing
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Authorized access with limit parameter") {
scenario("We get log cache with limit parameter only", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request with limit parameter")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("limit", "5"))
val response = makeGetRequest(request)
Then("We should get a successful response")
response.code should equal(200)
val json = response.body.extract[JObject]
And("The response should contain limited log entries")
val entries = (json \ "entries").extract[JArray]
entries.values.asInstanceOf[List[_]].size should be <= 5
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Authorized access with offset parameter") {
scenario("We get log cache with offset parameter only", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request with offset parameter")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("offset", "2"))
val response = makeGetRequest(request)
Then("We should get a successful response")
response.code should equal(200)
val json = response.body.extract[JObject]
And("The response should contain log entries starting from offset")
(json \ "entries") should not be JNothing
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Authorized access with both parameters") {
scenario("We get log cache with both limit and offset parameters", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request with both limit and offset parameters")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("limit", "3"), ("offset", "1"))
val response = makeGetRequest(request)
Then("We should get a successful response")
response.code should equal(200)
val json = response.body.extract[JObject]
And("The response should contain paginated log entries")
val entries = (json \ "entries").extract[JArray]
entries.values.asInstanceOf[List[_]].size should be <= 3
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Edge cases") {
scenario("We get error with zero limit (invalid parameter)", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request with zero limit")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("limit", "0"))
val response = makeGetRequest(request)
Then("We should get a not found response since endpoint does not exist")
response.code should equal(400)
val json = response.body.extract[JObject]
And("The response should contain the correct error message")
val message = (json \ "message").extract[String]
message should include("wrong value for obp_limit parameter")
}
scenario("We get log cache with large offset", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request with very large offset")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("offset", "10000"))
val response = makeGetRequest(request)
Then("We should get a successful response")
response.code should equal(200)
val json = response.body.extract[JObject]
And("The response should not fail")
val entries = (json \ "entries").extract[JArray]
entries.values.asInstanceOf[List[_]].size should be >= 0
}
scenario("We get log cache with minimum valid limit", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request with minimum valid limit (1)")
val request = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("limit", "1"))
val response = makeGetRequest(request)
Then("We should get a successful response")
response.code should equal(200)
val json = response.body.extract[JObject]
And("The response should contain at most 1 entry")
val entries = (json \ "entries").extract[JArray]
entries.values.asInstanceOf[List[_]].size should be <= 1
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Different log levels") {
scenario("We test different log levels with pagination", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make requests to different log levels with pagination")
val logLevels = List("debug", "info", "warning", "error", "all")
logLevels.foreach { logLevel =>
val request = (v5_1_0_Request / "system" / "log-cache" / logLevel).GET <@(user1) <<? List(("limit", "2"), ("offset", "0"))
val response = makeGetRequest(request)
Then(s"We should get successful response for log level $logLevel")
response.code should equal(200)
val json = response.body.extract[JObject]
And("The response should have the correct structure")
(json \ "entries") should not be JNothing
}
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Invalid log level") {
scenario("We get error for invalid log level", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We make a request with invalid log level")
val request = (v5_1_0_Request / "system" / "log-cache" / "invalid_level").GET <@(user1)
val response = makeGetRequest(request)
Then("We should get a not found response since endpoint does not exist")
response.code should equal(404)
}
}
feature(s"test $ApiEndpoint1 version $VersionOfApi - Invalid parameters") {
scenario("We test invalid pagination parameters", ApiEndpoint1, VersionOfApi) {
Given("We have a user with proper entitlement")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemLogCacheAll.toString)
When("We test with non-numeric limit parameter")
val requestInvalidLimit = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("limit", "abc"))
val responseInvalidLimit = makeGetRequest(requestInvalidLimit)
Then("We should get a not found response since endpoint does not exist")
responseInvalidLimit.code should equal(400)
When("We test with non-numeric offset parameter")
val requestInvalidOffset = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("offset", "xyz"))
val responseInvalidOffset = makeGetRequest(requestInvalidOffset)
Then("We should get a not found response since endpoint does not exist")
responseInvalidOffset.code should equal(400)
When("We test with negative limit parameter")
val requestNegativeLimit = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("limit", "-1"))
val responseNegativeLimit = makeGetRequest(requestNegativeLimit)
Then("We should get a not found response since endpoint does not exist")
responseNegativeLimit.code should equal(400)
When("We test with negative offset parameter")
val requestNegativeOffset = (v5_1_0_Request / "system" / "log-cache" / "info").GET <@(user1) <<? List(("offset", "-1"))
val responseNegativeOffset = makeGetRequest(requestNegativeOffset)
Then("We should get a not found response since endpoint does not exist")
responseNegativeOffset.code should equal(400)
}
}
}