/
DeleteListItemsRouteADME2ESpec.scala
180 lines (159 loc) · 7.79 KB
/
DeleteListItemsRouteADME2ESpec.scala
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
/*
* Copyright © 2015-2021 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.e2e.admin.lists
import akka.actor.ActorSystem
import akka.http.scaladsl.model.headers.BasicHttpCredentials
import akka.http.scaladsl.model.{HttpResponse, StatusCodes}
import akka.http.scaladsl.testkit.RouteTestTimeout
import com.typesafe.config.{Config, ConfigFactory}
import org.knora.webapi.E2ESpec
import org.knora.webapi.e2e.{ClientTestDataCollector, TestDataFileContent, TestDataFilePath}
import org.knora.webapi.messages.admin.responder.listsmessages._
import org.knora.webapi.messages.store.triplestoremessages.{RdfDataObject, TriplestoreJsonProtocol}
import org.knora.webapi.messages.v1.responder.sessionmessages.SessionJsonProtocol
import org.knora.webapi.messages.v1.routing.authenticationmessages.CredentialsADM
import org.knora.webapi.sharedtestdata.{SharedListsTestDataADM, SharedTestDataADM}
import org.knora.webapi.util.AkkaHttpUtils
import scala.concurrent.duration._
object DeleteListItemsRouteADME2ESpec {
val config: Config = ConfigFactory.parseString("""
akka.loglevel = "DEBUG"
akka.stdout-loglevel = "DEBUG"
""".stripMargin)
}
/**
* End-to-End (E2E) test specification for testing endpoint.
*/
class DeleteListItemsRouteADME2ESpec
extends E2ESpec(DeleteListItemsRouteADME2ESpec.config)
with SessionJsonProtocol
with TriplestoreJsonProtocol
with ListADMJsonProtocol {
implicit def default(implicit system: ActorSystem): RouteTestTimeout = RouteTestTimeout(5.seconds)
// Directory path for generated client test data
private val clientTestDataPath: Seq[String] = Seq("admin", "lists")
// Collects client test data
private val clientTestDataCollector = new ClientTestDataCollector(settings)
override lazy val rdfDataObjects = List(
RdfDataObject(path = "test_data/demo_data/images-demo-data.ttl", name = "http://www.knora.org/data/00FF/images"),
RdfDataObject(path = "test_data/all_data/anything-data.ttl", name = "http://www.knora.org/data/0001/anything")
)
val rootCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.rootUser,
"test"
)
val normalUserCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.normalUser,
"test"
)
val anythingUserCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.anythingUser1,
"test"
)
val anythingAdminUserCreds: CredentialsADM = CredentialsADM(
SharedTestDataADM.anythingAdminUser,
"test"
)
private val treeListInfo: ListRootNodeInfoADM = SharedListsTestDataADM.treeListInfo
private val treeListNodes: Seq[ListChildNodeADM] = SharedListsTestDataADM.treeListChildNodes
"The List Items Route (/admin/lists)" when {
"deleting list items" should {
"return forbidden exception when requesting user is not system or project admin" in {
val encodedNodeUrl = java.net.URLEncoder.encode(SharedListsTestDataADM.otherTreeListInfo.id, "utf-8")
val request = Delete(baseApiUrl + s"/admin/lists/" + encodedNodeUrl) ~> addCredentials(
BasicHttpCredentials(anythingUserCreds.user.email, anythingUserCreds.password))
val response: HttpResponse = singleAwaitingRequest(request)
response.status should be(StatusCodes.Forbidden)
}
"delete first of two child node and remaining child" in {
val encodedNodeUrl = java.net.URLEncoder.encode("http://rdfh.ch/lists/0001/notUsedList0141", "utf-8")
val request = Delete(baseApiUrl + s"/admin/lists/" + encodedNodeUrl) ~> addCredentials(
BasicHttpCredentials(anythingAdminUserCreds.user.email, anythingAdminUserCreds.password))
val response: HttpResponse = singleAwaitingRequest(request)
response.status should be(StatusCodes.OK)
val node = AkkaHttpUtils.httpResponseToJson(response).fields("node").convertTo[ListNodeADM]
node.getNodeId should be("http://rdfh.ch/lists/0001/notUsedList014")
val children = node.getChildren
children.size should be(1)
// last child must be shifted one place to left
val leftChild = children.head
leftChild.id should be("http://rdfh.ch/lists/0001/notUsedList0142")
leftChild.position should be(0)
}
"delete a middle node and shift its siblings" in {
val encodedNodeUrl = java.net.URLEncoder.encode("http://rdfh.ch/lists/0001/notUsedList02", "utf-8")
val request = Delete(baseApiUrl + s"/admin/lists/" + encodedNodeUrl) ~> addCredentials(
BasicHttpCredentials(anythingAdminUserCreds.user.email, anythingAdminUserCreds.password))
val response: HttpResponse = singleAwaitingRequest(request)
response.status should be(StatusCodes.OK)
val node = AkkaHttpUtils.httpResponseToJson(response).fields("node").convertTo[ListNodeADM]
val children = node.getChildren
children.size should be(2)
// last child must be shifted one place to left
val lastChild = children.last
lastChild.id should be("http://rdfh.ch/lists/0001/notUsedList03")
lastChild.position should be(1)
lastChild.children.size should be(1)
// first child must have its child
val firstChild = children.head
firstChild.children.size should be(5)
clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = "delete-list-node-response",
fileExtension = "json"
),
text = responseToString(response)
)
)
}
"delete the single child of a node" in {
val encodedNodeUrl = java.net.URLEncoder.encode("http://rdfh.ch/lists/0001/notUsedList031", "utf-8")
val request = Delete(baseApiUrl + s"/admin/lists/" + encodedNodeUrl) ~> addCredentials(
BasicHttpCredentials(anythingAdminUserCreds.user.email, anythingAdminUserCreds.password))
val response: HttpResponse = singleAwaitingRequest(request)
response.status should be(StatusCodes.OK)
val node = AkkaHttpUtils.httpResponseToJson(response).fields("node").convertTo[ListNodeADM]
node.getNodeId should be("http://rdfh.ch/lists/0001/notUsedList03")
val children = node.getChildren
children.size should be(0)
}
}
"delete a list entirely with all its children" in {
val encodedNodeUrl = java.net.URLEncoder.encode("http://rdfh.ch/lists/0001/notUsedList", "utf-8")
val request = Delete(baseApiUrl + s"/admin/lists/" + encodedNodeUrl) ~> addCredentials(
BasicHttpCredentials(anythingAdminUserCreds.user.email, anythingAdminUserCreds.password))
val response: HttpResponse = singleAwaitingRequest(request)
response.status should be(StatusCodes.OK)
val deletedStatus = AkkaHttpUtils.httpResponseToJson(response).fields("deleted")
deletedStatus.convertTo[Boolean] should be(true)
clientTestDataCollector.addFile(
TestDataFileContent(
filePath = TestDataFilePath(
directoryPath = clientTestDataPath,
filename = "delete-list-response",
fileExtension = "json"
),
text = responseToString(response)
)
)
}
}
}