Skip to content

Commit c3424dc

Browse files
committed
fix error code
1 parent 7b7a7a4 commit c3424dc

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

mobile-save-for-later/src/main/scala/com/gu/sfl/controller/SaveArticlesController.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ class SaveArticlesController(updateSavedArticles: UpdateSavedArticles)(implicit
2626
case Failure(t) => {
2727
val headersWithoutAuth = lambdaRequest.headers.filter{ case (k,v) => headersToKeep.contains(k.toLowerCase)}
2828
logger.warn(s"Could not read value: $json \nWith headers: $headersWithoutAuth" )
29+
Future { LambdaResponse(StatusCodes.badRequest, Some("Could not parse request body")) }
2930
}
30-
31-
case _ => ()
31+
case _ => futureSave(triedSavedArticles, lambdaRequest.headers)
3232
}
33-
futureSave(triedSavedArticles, lambdaRequest.headers)
3433
case LambdaRequest(None, _) =>
3534
Future { LambdaResponse(StatusCodes.badRequest, Some("Expected a json body")) }
3635
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.gu.sfl.controller
2+
3+
import com.gu.sfl.exception.SaveForLaterError
4+
import com.gu.sfl.lambda.LambdaRequest
5+
import com.gu.sfl.model.SavedArticles
6+
import com.gu.sfl.savedarticles.UpdateSavedArticles
7+
import com.gu.sfl.util.StatusCodes
8+
import org.specs2.mock.Mockito
9+
import org.specs2.mutable.Specification
10+
import org.specs2.specification.Scope
11+
12+
import scala.concurrent.duration.Duration
13+
import scala.concurrent.{Await, ExecutionContext, Future}
14+
15+
class SaveArticlesControllerSpec extends Specification with Mockito {
16+
17+
"SaveArticlesController" should {
18+
19+
"return a 400 when the date on an article is not in the expected format" in new Setup {
20+
val json = """{"version":"1","articles":[{"id":"id/1","shortUrl":"p/1","date":"2026-07-16T10:15:30.123Z","read":false}]}"""
21+
val response = Await.result(controller(LambdaRequest(Some(json))), Duration.Inf)
22+
23+
response.statusCode mustEqual StatusCodes.badRequest
24+
there were no(updateSavedArticles).save(any[Map[String, String]](), any[SavedArticles]())
25+
}
26+
27+
"return a 400 when the request body is not valid json" in new Setup {
28+
val response = Await.result(controller(LambdaRequest(Some("not json"))), Duration.Inf)
29+
30+
response.statusCode mustEqual StatusCodes.badRequest
31+
}
32+
33+
"return a 400 when there is no request body" in new Setup {
34+
val response = Await.result(controller(LambdaRequest(None)), Duration.Inf)
35+
36+
response.statusCode mustEqual StatusCodes.badRequest
37+
}
38+
39+
"attempt to save the articles when the date is in the expected format" in new Setup {
40+
val json = """{"version":"1","articles":[{"id":"id/1","shortUrl":"p/1","date":"2026-07-16T10:15:30Z","read":false}]}"""
41+
updateSavedArticles.save(any[Map[String, String]](), any[SavedArticles]()) returns Future.successful(Left(mock[SaveForLaterError]))
42+
43+
Await.result(controller(LambdaRequest(Some(json))), Duration.Inf)
44+
45+
there was one(updateSavedArticles).save(any[Map[String, String]](), any[SavedArticles]())
46+
}
47+
}
48+
49+
trait Setup extends Scope {
50+
implicit val executionContext: ExecutionContext = scala.concurrent.ExecutionContext.global
51+
val updateSavedArticles = mock[UpdateSavedArticles]
52+
val controller = new SaveArticlesController(updateSavedArticles)
53+
}
54+
}

0 commit comments

Comments
 (0)