Skip to content

Commit 5ab0870

Browse files
authored
unit tests for feat comment on task (BE) (#60)
1 parent cfe087e commit 5ab0870

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package controllers
2+
3+
import dto.request.comment.{CreateUpdateCommentRequest, TextChunk}
4+
import dto.request.project.CreateProjectRequest
5+
import dto.request.task.CreateTaskRequest
6+
import dto.request.workspace.CreateWorkspaceRequest
7+
import exception.AppException
8+
import org.scalatest.BeforeAndAfterAll
9+
import org.scalatest.concurrent.ScalaFutures
10+
import org.scalatestplus.play._
11+
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
12+
import play.api.inject.guice.GuiceApplicationBuilder
13+
import play.api.libs.json.Json
14+
import play.api.mvc.Cookie
15+
import play.api.test.Helpers._
16+
import play.api.test._
17+
import play.api.{Application, Configuration}
18+
import services._
19+
20+
class CommentControllerSpec
21+
extends PlaySpec
22+
with GuiceOneAppPerSuite
23+
with Injecting
24+
with ScalaFutures
25+
with BeforeAndAfterAll {
26+
27+
override implicit def fakeApplication(): Application = {
28+
new GuiceApplicationBuilder()
29+
.configure(
30+
"config.resource" -> "application.test.conf",
31+
"slick.dbs.default.db.url" -> s"jdbc:h2:mem:comment_test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL;DATABASE_TO_UPPER=false"
32+
)
33+
.build()
34+
}
35+
36+
// config & token
37+
lazy val config: Configuration = app.configuration
38+
lazy val defaultAdminEmail: String =
39+
config.getOptional[String]("admin.email").getOrElse("admin@mail.com")
40+
lazy val defaultAdminName: String =
41+
config.getOptional[String]("admin.name").getOrElse("Administrator")
42+
lazy val cookieName: String =
43+
config.getOptional[String]("cookie.name").getOrElse("auth_token")
44+
45+
def fakeToken: String = {
46+
val jwtService = inject[JwtService]
47+
jwtService
48+
.generateToken(UserToken(1, defaultAdminName, defaultAdminEmail))
49+
.getOrElse(throw new RuntimeException("JWT token not generated"))
50+
}
51+
52+
override def beforeAll(): Unit = {
53+
val workspaceService = inject[WorkspaceService]
54+
val projectService = inject[ProjectService]
55+
val taskService = inject[TaskService]
56+
57+
await(
58+
workspaceService.createWorkspace(
59+
CreateWorkspaceRequest("Workspace test"),
60+
1
61+
)
62+
)
63+
64+
await(
65+
// create project with default columns
66+
projectService.createProject(
67+
CreateProjectRequest("Project test"),
68+
1,
69+
1
70+
)
71+
)
72+
73+
await(
74+
// create tasks in column
75+
taskService.createNewTask(
76+
CreateTaskRequest("Project test", 1),
77+
1,
78+
1
79+
)
80+
)
81+
}
82+
83+
"CommentController" should {
84+
85+
"create comment successfully" in {
86+
val commentChunk = TextChunk("test comment")
87+
val body = Json.toJson(CreateUpdateCommentRequest(
88+
Seq(commentChunk)
89+
))
90+
val request = FakeRequest(POST, "/api/tasks/1/comments")
91+
.withCookies(Cookie(cookieName, fakeToken))
92+
.withBody(body)
93+
94+
val result = route(app, request).get
95+
96+
status(result) mustBe CREATED
97+
(contentAsJson(result) \ "message").as[String] must include(
98+
"Comment created successfully"
99+
)
100+
}
101+
102+
"fail when creating comment with unexisting task id" in {
103+
val commentChunk = TextChunk("test comment")
104+
val body = Json.toJson(CreateUpdateCommentRequest(
105+
Seq(commentChunk)
106+
))
107+
val request = FakeRequest(POST, "/api/tasks/2/comments")
108+
.withCookies(Cookie(cookieName, fakeToken))
109+
.withBody(body)
110+
111+
val ex = intercept[AppException] {
112+
await(route(app, request).get)
113+
}
114+
ex.statusCode mustBe NOT_FOUND
115+
ex.message must include(
116+
"Task with ID 2 does not exist or you do not have access"
117+
)
118+
}
119+
120+
"update comment successfully" in {
121+
val commentChunk = TextChunk("updated comment")
122+
val body = Json.toJson(CreateUpdateCommentRequest(
123+
Seq(commentChunk)
124+
))
125+
val request = FakeRequest(PUT, "/api/comments/1")
126+
.withCookies(Cookie(cookieName, fakeToken))
127+
.withBody(body)
128+
.withHeaders(CONTENT_TYPE -> "application/json")
129+
130+
val result = route(app, request).get
131+
132+
status(result) mustBe OK
133+
(contentAsJson(result) \ "message")
134+
.as[String] mustBe "Comment updated successfully"
135+
}
136+
137+
"fail when updating comment with unexist comment id" in {
138+
val commentChunk = TextChunk("error comment")
139+
val body = Json.toJson(CreateUpdateCommentRequest(
140+
Seq(commentChunk)
141+
))
142+
val request = FakeRequest(PUT, "/api/comments/0")
143+
.withCookies(Cookie(cookieName, fakeToken))
144+
.withBody(body)
145+
.withHeaders(CONTENT_TYPE -> "application/json")
146+
147+
val ex = intercept[AppException] {
148+
await(route(app, request).get)
149+
}
150+
151+
ex.statusCode mustBe NOT_FOUND
152+
ex.message must include("Comment with ID 0 does not exist")
153+
}
154+
155+
"get comments in task by task id successfully" in {
156+
val request = FakeRequest(GET, "/api/tasks/1/comments")
157+
.withCookies(Cookie(cookieName, fakeToken))
158+
val result = route(app, request).get
159+
160+
status(result) mustBe OK
161+
(contentAsJson(result) \ "message")
162+
.as[String] mustBe "Comments retrieved successfully for task ID: 1"
163+
}
164+
165+
"fail when getting comments in a task with unexist task id" in {
166+
val request = FakeRequest(GET, "/api/tasks/0/comments")
167+
.withCookies(Cookie(cookieName, fakeToken))
168+
169+
val ex = intercept[AppException] {
170+
await(route(app, request).get)
171+
}
172+
173+
ex.statusCode mustBe NOT_FOUND
174+
ex.message must include("Task with ID 0 does not exist or you do not have access.")
175+
}
176+
177+
"delete comment successfully" in {
178+
val request = FakeRequest(DELETE, "/api/comments/1")
179+
.withCookies(Cookie(cookieName, fakeToken))
180+
val result = route(app, request).get
181+
182+
status(result) mustBe OK
183+
(contentAsJson(result) \ "message")
184+
.as[String] mustBe "Comment deleted successfully"
185+
}
186+
187+
"fail when deleting comment with unexist id" in {
188+
val request = FakeRequest(DELETE, "/api/comments/0")
189+
.withCookies(Cookie(cookieName, fakeToken))
190+
val result = route(app, request).get
191+
192+
val ex = intercept[AppException] {
193+
await(result)
194+
}
195+
ex.statusCode mustBe NOT_FOUND
196+
ex.message must include("Comment with ID 0 does not exist.")
197+
}
198+
199+
200+
}
201+
}

0 commit comments

Comments
 (0)