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