Skip to content

Commit

Permalink
Add test that covers the missing username path
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinJuricev committed Oct 31, 2023
1 parent 85eb393 commit 64dbe4d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 55 deletions.
6 changes: 2 additions & 4 deletions src/main/kotlin/io/github/nomisrev/repo/UserPersistence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ fun userPersistence(
ensureNotNull(info) { UserNotFound("userId=$userId") }
}

override suspend fun unfollowProfile(
followedUsername: String,
followerId: UserId
): Unit = followingQueries.delete(followedUsername, followerId.serial)
override suspend fun unfollowProfile(followedUsername: String, followerId: UserId): Unit =
followingQueries.delete(followedUsername, followerId.serial)

private fun generateSalt(): ByteArray = UUID.randomUUID().toString().toByteArray()

Expand Down
122 changes: 71 additions & 51 deletions src/test/kotlin/io/github/nomisrev/routes/ProfileRouteSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,83 @@ import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.ktor.client.call.body
import io.ktor.client.plugins.resources.bearerAuth
import io.ktor.client.plugins.resources.delete
import io.ktor.client.plugins.resources.get
import io.ktor.client.request.bearerAuth
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType

class ProfileRouteSpec :
StringSpec({
val validUsername = "username"
val validEmail = "[email protected]"
val validPw = "123456789"
val validUsernameFollowed = "username2"
val validEmailFollowed = "[email protected]"

"Can unfollow profile" {
withServer { dependencies ->
val token = dependencies.userService
.register(RegisterUser(validUsername, validEmail, validPw))
.shouldBeRight()
dependencies.userService
.register(RegisterUser(validUsernameFollowed, validEmailFollowed, validPw))
.shouldBeRight()

val response = delete(ProfilesResource.Follow(username = validUsernameFollowed)) {
bearerAuth(token.value)
}

response.status shouldBe HttpStatusCode.OK
with(response.body<ProfileWrapper<Profile>>().profile) {
username shouldBe validUsernameFollowed
bio shouldBe ""
image shouldBe ""
following shouldBe false
}
}
}
val validUsername = "username"
val validEmail = "[email protected]"
val validPw = "123456789"
val validUsernameFollowed = "username2"
val validEmailFollowed = "[email protected]"

"Needs token to unfollow" {
withServer {
val response = delete(ProfilesResource.Follow(username = validUsernameFollowed))
"Can unfollow profile" {
withServer { dependencies ->
val token =
dependencies.userService
.register(RegisterUser(validUsername, validEmail, validPw))
.shouldBeRight()
dependencies.userService
.register(RegisterUser(validUsernameFollowed, validEmailFollowed, validPw))
.shouldBeRight()

response.status shouldBe HttpStatusCode.Unauthorized
}
val response =
delete(ProfilesResource.Follow(username = validUsernameFollowed)) {
bearerAuth(token.value)
}

response.status shouldBe HttpStatusCode.OK
with(response.body<ProfileWrapper<Profile>>().profile) {
username shouldBe validUsernameFollowed
bio shouldBe ""
image shouldBe ""
following shouldBe false
}
}
}

"Username invalid to unfollow" {
withServer { dependencies ->
val token = dependencies.userService
.register(RegisterUser(validUsername, validEmail, validPw))
.shouldBeRight()
"Needs token to unfollow" {
withServer {
val response = delete(ProfilesResource.Follow(username = validUsernameFollowed))

val response = delete(ProfilesResource.Follow(username = validUsernameFollowed)) {
bearerAuth(token.value)
}
response.status shouldBe HttpStatusCode.Unauthorized
}
}

response.status shouldBe HttpStatusCode.UnprocessableEntity
}
}
"Username invalid to unfollow" {
withServer { dependencies ->
val token =
dependencies.userService
.register(RegisterUser(validUsername, validEmail, validPw))
.shouldBeRight()

val response =
delete(ProfilesResource.Follow(username = validUsernameFollowed)) {
bearerAuth(token.value)
}

response.status shouldBe HttpStatusCode.UnprocessableEntity
}
}

"Get profile with no following" {
withServer { dependencies: Dependencies ->
dependencies.userService
.register(RegisterUser(userName, validEmail, validPw))
.register(RegisterUser(validUsername, validEmail, validPw))
.shouldBeRight()
val response =
get(ProfileResource.Username(username = userName)) {
get(ProfilesResource.Username(username = validUsername)) {
contentType(ContentType.Application.Json)
}

response.status shouldBe HttpStatusCode.OK
with(response.body<Profile>()) {
username shouldBe userName
username shouldBe validUsername
bio shouldBe ""
image shouldBe ""
following shouldBe false
Expand All @@ -87,13 +94,26 @@ class ProfileRouteSpec :
"Get profile invalid username" {
withServer {
val response =
get(ProfileResource.Username(username = userName)) {
get(ProfilesResource.Username(username = validUsername)) {
contentType(ContentType.Application.Json)
}

response.status shouldBe HttpStatusCode.UnprocessableEntity
response.body<GenericErrorModel>().errors.body shouldBe
listOf("User with username=$validUsername not found")
}
}

"Get profile by username missing username" {
withServer {
val response =
get(ProfilesResource.Username(username = "")) {
contentType(ContentType.Application.Json)
}

response.status shouldBe HttpStatusCode.UnprocessableEntity
response.body<GenericErrorModel>().errors.body shouldBe
listOf("User with username=$userName not found")
listOf("Missing username parameter in request")
}
}
})
})

0 comments on commit 64dbe4d

Please sign in to comment.