Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

Commit c2d978c

Browse files
authored
Fix websocket issues (#116)
* Always send current item * Possible fix for account confirmation error * Remove authentication for GET Poll endpoint * Fix cucumber test * Update UserStepDefinitions.kt * Update Livepoll.postman_collection.json
1 parent ea30d85 commit c2d978c

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

postman/Livepoll.postman_collection.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info": {
3-
"_postman_id": "98a6fefc-8013-405c-ac74-c578a47251b5",
3+
"_postman_id": "b83df12e-5482-4bc6-baa9-c95b38afdbb2",
44
"name": "Livepoll",
55
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
66
},
@@ -2199,8 +2199,8 @@
21992199
" headers: 'Cookie:'+pm.environment.get(\"cookies\")\r",
22002200
" }, function(error, response) {\r",
22012201
" pm.test(\"Poll item does not exist anymore\", function() {\r",
2202-
" pm.expect(response).to.have.property('code', 403)\r",
2203-
" pm.expect(response).to.have.property('status', 'Forbidden')\r",
2202+
" pm.expect(response).to.have.property('code', 404)\r",
2203+
" pm.expect(response).to.have.property('status', 'Not Found')\r",
22042204
" })\r",
22052205
"})\r",
22062206
"\r",

src/main/kotlin/de/livepoll/api/config/SecurityConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SecurityConfig(
2727
.antMatchers("/v1/account/register").permitAll()
2828
.antMatchers("/v1/account/confirm").permitAll()
2929
.antMatchers("/v1/account/login").permitAll()
30+
.antMatchers("/v1/polls/{id:[\\d]+}").permitAll()
3031
.antMatchers("/v1/websocket/**").permitAll()
3132
.antMatchers("/actuator/**").permitAll()
3233
//.antMatchers("/admin").hasRole("ADMIN") // TODO: introduce ROLE_ADMIN authority later on

src/main/kotlin/de/livepoll/api/controller/PollController.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ class PollController(
3636

3737
@ApiOperation(value = "Get poll", tags = ["Poll"])
3838
@GetMapping("/{id}")
39-
fun getPoll(@PathVariable(name = "id") pollId: Long, @AuthenticationPrincipal user: User): PollDtoOut {
40-
accountService.checkAuthorizationByPollId(pollId)
39+
fun getPoll(@PathVariable(name = "id") pollId: Long): PollDtoOut {
4140
return pollService.getPoll(pollId)
4241
}
4342

src/main/kotlin/de/livepoll/api/service/AccountService.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import de.livepoll.api.util.jwtCookie.CookieCipher
1313
import de.livepoll.api.util.jwtCookie.CookieUtil
1414
import org.springframework.context.ApplicationEventPublisher
1515
import org.springframework.dao.DataAccessException
16+
import org.springframework.dao.EmptyResultDataAccessException
1617
import org.springframework.http.HttpHeaders
1718
import org.springframework.http.HttpStatus
1819
import org.springframework.http.ResponseEntity
@@ -86,14 +87,19 @@ class AccountService(
8687
* @param token the token string to confirm the new account
8788
*/
8889
fun confirmAccount(token: String): Boolean {
89-
val verificationToken = verificationTokenRepository.findByToken(token)
90-
if (verificationToken.expiryDate.after(Date())) {
91-
val user = userRepository.findByUsername(verificationToken.username)
92-
user?.isAccountEnabled = true
93-
verificationTokenRepository.delete(verificationToken)
94-
return true
90+
try {
91+
val verificationToken = verificationTokenRepository.findByToken(token)
92+
if (verificationToken.expiryDate.after(Date())) {
93+
val user = userRepository.findByUsername(verificationToken.username)
94+
user?.isAccountEnabled = true
95+
verificationTokenRepository.delete(verificationToken)
96+
return true
97+
}
98+
return false
99+
} catch (ex:EmptyResultDataAccessException) {
100+
return false
95101
}
96-
return false
102+
97103
}
98104

99105
private fun calculateExpiryDate() = Calendar.getInstance()

src/main/kotlin/de/livepoll/api/service/PollService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ class PollService(
180180
}
181181

182182
if (this.currentItem != null) {
183-
webSocketService.sendCurrentItem(this.slug, this.id, this.currentItem)
184183
webSocketService.sendItemWithAnswers(this.currentItem!!)
185184
}
186185

186+
webSocketService.sendCurrentItem(this.slug, this.id, this.currentItem)
187187
return pollRepository.saveAndFlush(this).toDtoOut()
188188
}
189189
}

src/test/kotlin/de/livepoll/api/cucumber/stepdefinitions/UserStepDefinitions.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package de.livepoll.api.cucumber.stepdefinitions
22

33
import de.livepoll.api.cucumber.CucumberIntegrationTest
4+
import de.livepoll.api.entity.db.OpenTextItem
45
import de.livepoll.api.entity.db.Poll
56
import de.livepoll.api.entity.db.PollItem
67
import de.livepoll.api.entity.db.User
8+
import de.livepoll.api.repository.OpenTextItemRepository
79
import de.livepoll.api.repository.PollRepository
810
import de.livepoll.api.repository.UserRepository
911
import io.cucumber.java.en.And
@@ -20,11 +22,13 @@ private const val LOGOUT_ENDPOINT = "/v1/account/logout"
2022

2123
class UserStepDefinitions(
2224
private val pollRepository: PollRepository,
23-
private val userRepository: UserRepository
25+
private val userRepository: UserRepository,
26+
private val openTextItemRepository: OpenTextItemRepository
2427
) : CucumberIntegrationTest(userRepository) {
2528

2629
private val USER_ENDPOINT = "/v1/user"
2730
private val POLL_ENDPOINT = "/v1/polls"
31+
private val POLL_ITEM_ENDPOINT = "/v1/poll-items"
2832

2933
lateinit var status: HttpStatus
3034
var alreadyConfirmed = false
@@ -87,8 +91,9 @@ class UserStepDefinitions(
8791
)
8892
)
8993
}
90-
val id = pollRepository.findBySlug("different_user_test_slug")!!.id
91-
val url = "${SERVER_URL}:$port$POLL_ENDPOINT/${id}"
94+
val poll = pollRepository.findBySlug("different_user_test_slug")!!
95+
val id = openTextItemRepository.saveAndFlush(OpenTextItem(0, poll, "question", 0, mutableListOf())).id
96+
val url = "${SERVER_URL}:$port$POLL_ITEM_ENDPOINT/${id}"
9297
try {
9398
val pollResponseEntity = makeGetRequestWithSessionCookie<Any>(url, SessionCookieUtil.sessionCookie)
9499
assertThat(pollResponseEntity.statusCode).isEqualTo(HttpStatus.FORBIDDEN)

0 commit comments

Comments
 (0)