<commit_before>package controllers.save_for_later

import controllers.ClaimScenarioFactory
import models.view.cache.EncryptedCacheHandling
import models.{DayMonthYear, NationalInsuranceNumber}
import models.domain.{YourDetails, Claim, Claiming}
import models.view.{CachedClaim}
import play.api.test.FakeRequest
import play.api.test.Helpers._
import utils.{LightFakeApplication, WithApplication, WithJsBrowser}
import utils.pageobjects.s_claim_date.GClaimDatePage
import utils.pageobjects.{Page, PageObjectsContext}
import utils.pageobjects.s_about_you.{GContactDetailsPage}
import utils.pageobjects.save_for_later.GSaveForLaterSavePage
import org.specs2.mutable._
import utils.pageobjects.PageObjects


class GSaveForLaterSaveIntegrationSpec extends Specification {

  "Save for later page" should {
    "be shown after clicking Save in nationality" in new WithJsBrowser with PageObjects {
      loadClaimData(context)

      val page = GContactDetailsPage(context)
      page goToThePage()
      page fillPageWith SaveForLaterScenarioFactory.WithEmailSet()

      val nationalityPage = page.clickLinkOrButton("#next")
      nationalityPage.source must contain("Save for later")
      nationalityPage.source must contain("id=\"save\"")

      val savePage=nationalityPage.clickLinkOrButton("#save")
      savePage.url mustEqual GSaveForLaterSavePage.url
      savePage.source must contain("Your progress has been saved")
      savePage.source must contain("Continue your application" )
      savePage.source must contain("id=\"continue\"")
    }

    "return back to original screen after clicking continue your application" in new WithJsBrowser with PageObjects {
      loadClaimData(context)

      val page = GContactDetailsPage(context)
      page goToThePage()
      page fillPageWith SaveForLaterScenarioFactory.WithEmailSet()

      val nationalityPage = page.clickLinkOrButton("#next")
      nationalityPage.source must contain("Save for later")
      nationalityPage.source must contain("id=\"save\"")

      val savePage=nationalityPage.clickLinkOrButton("#save")
      savePage.url mustEqual GSaveForLaterSavePage.url
      savePage.source must contain("Your progress has been saved")
      savePage.source must contain("Continue your application" )
      savePage.source must contain("id=\"continue\"")

      /* ODDLY Clicking Continue button causes test to crash
      println("================ save page ==========")
      println(savePage.source)
      val nationalityPageAgain=savePage.clickLinkOrButton("#continue")
      println( "=================== Nationality page again:")
      println(nationalityPageAgain.source)

      //
      // nationalityPageAgain.url mustEqual GNationalityAndResidencyPage.url

      Needs more investigation. ColinG 07/12/15.
      */
    }


    "contain link to gov page" in new WithJsBrowser with PageObjects {
      loadClaimData(context)

      val page = GContactDetailsPage(context)
      page goToThePage()
      page fillPageWith SaveForLaterScenarioFactory.WithEmailSet()

      val nationalityPage = page.clickLinkOrButton("#next")
      nationalityPage.source must contain("Save for later")
      nationalityPage.source must contain("id=\"save\"")

      val savePage=nationalityPage.clickLinkOrButton("#save")
      savePage.url mustEqual GSaveForLaterSavePage.url
      savePage.source must contain("Your progress has been saved")
      savePage.source must contain("Go to GOV.UK" )
      savePage.source must contain("id=\"govuk\"" )
    }

    "return ok resume screen when claim is found in sfl cache" in new WithApplication(app = LightFakeApplication(additionalConfiguration = Map("saveForLaterResumeEnabled" -> "true"))) with Claiming {
      var claim = new Claim(CachedClaim.key, uuid="123456")
      val details = new YourDetails("",None, "",None, "green",NationalInsuranceNumber(Some("AB123456D")), DayMonthYear(None, None, None))
      claim=claim+details
      val encryptedCacheHandling = new EncryptedCacheHandling() { val cacheKey = "123456" }
      encryptedCacheHandling.saveForLaterInCache(claim, "/lastlocation")

      val request=FakeRequest(GET, "?savekey=123456")
      val result = GResume.present(request)
      val bodyText: String = contentAsString(result)
      status(result) mustEqual OK
      bodyText must contain( "Enter your details to resume your application")
    }


    "contain cookie for saved application version when resumed" in new WithJsBrowser  with PageObjects{
      loadClaimData(context)
/*<commit_msg>CADS-474 : Save for later link to resume must not contain uuid in clear. Parameter amended from savekey to x<commit_after>package controllers.save_for_later

import controllers.ClaimScenarioFactory
import models.view.cache.EncryptedCacheHandling
import models.{DayMonthYear, NationalInsuranceNumber}
import models.domain.{YourDetails, Claim, Claiming}
import models.view.{CachedClaim}
import play.api.test.FakeRequest
import play.api.test.Helpers._
import utils.{LightFakeApplication, WithApplication, WithJsBrowser}
import utils.pageobjects.s_claim_date.GClaimDatePage
import utils.pageobjects.{Page, PageObjectsContext}
import utils.pageobjects.s_about_you.{GContactDetailsPage}
import utils.pageobjects.save_for_later.GSaveForLaterSavePage
import org.specs2.mutable._
import utils.pageobjects.PageObjects


class GSaveForLaterSaveIntegrationSpec extends Specification {

  "Save for later page" should {
    "be shown after clicking Save in nationality" in new WithJsBrowser with PageObjects {
      loadClaimData(context)

      val page = GContactDetailsPage(context)
      page goToThePage()
      page fillPageWith SaveForLaterScenarioFactory.WithEmailSet()

      val nationalityPage = page.clickLinkOrButton("#next")
      nationalityPage.source must contain("Save for later")
      nationalityPage.source must contain("id=\"save\"")

      val savePage=nationalityPage.clickLinkOrButton("#save")
      savePage.url mustEqual GSaveForLaterSavePage.url
      savePage.source must contain("Your progress has been saved")
      savePage.source must contain("Continue your application" )
      savePage.source must contain("id=\"continue\"")
    }

    "return back to original screen after clicking continue your application" in new WithJsBrowser with PageObjects {
      loadClaimData(context)

      val page = GContactDetailsPage(context)
      page goToThePage()
      page fillPageWith SaveForLaterScenarioFactory.WithEmailSet()

      val nationalityPage = page.clickLinkOrButton("#next")
      nationalityPage.source must contain("Save for later")
      nationalityPage.source must contain("id=\"save\"")

      val savePage=nationalityPage.clickLinkOrButton("#save")
      savePage.url mustEqual GSaveForLaterSavePage.url
      savePage.source must contain("Your progress has been saved")
      savePage.source must contain("Continue your application" )
      savePage.source must contain("id=\"continue\"")

      /* ODDLY Clicking Continue button causes test to crash
      println("================ save page ==========")
      println(savePage.source)
      val nationalityPageAgain=savePage.clickLinkOrButton("#continue")
      println( "=================== Nationality page again:")
      println(nationalityPageAgain.source)

      //
      // nationalityPageAgain.url mustEqual GNationalityAndResidencyPage.url

      Needs more investigation. ColinG 07/12/15.
      */
    }


    "contain link to gov page" in new WithJsBrowser with PageObjects {
      loadClaimData(context)

      val page = GContactDetailsPage(context)
      page goToThePage()
      page fillPageWith SaveForLaterScenarioFactory.WithEmailSet()

      val nationalityPage = page.clickLinkOrButton("#next")
      nationalityPage.source must contain("Save for later")
      nationalityPage.source must contain("id=\"save\"")

      val savePage=nationalityPage.clickLinkOrButton("#save")
      savePage.url mustEqual GSaveForLaterSavePage.url
      savePage.source must contain("Your progress has been saved")
      savePage.source must contain("Go to GOV.UK" )
      savePage.source must contain("id=\"govuk\"" )
    }

    "return ok resume screen when claim is found in sfl cache" in new WithApplication(app = LightFakeApplication(additionalConfiguration = Map("saveForLaterResumeEnabled" -> "true"))) with Claiming {
      var claim = new Claim(CachedClaim.key, uuid="123456")
      val details = new YourDetails("",None, "",None, "green",NationalInsuranceNumber(Some("AB123456D")), DayMonthYear(None, None, None))
      claim=claim+details
      val encryptedCacheHandling = new EncryptedCacheHandling() { val cacheKey = "123456" }
      encryptedCacheHandling.saveForLaterInCache(claim, "/lastlocation")

      val request=FakeRequest(GET, "?x=123456")
      val result = GResume.present(request)
      val bodyText: String = contentAsString(result)
      status(result) mustEqual OK
      bodyText must contain( "Enter your details to resume your application")
    }


    "contain cookie for saved application version when resumed" in new WithJsBrowser  with PageObjects{
      loadClaimData(context)
/*
{'fraction_non_alphanumeric': 0.09531644181199957, 'fraction_numerical': 0.0071295382252934076, 'mean_word_length': 4.307334109429569, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6722331', 'n_tokens_mistral': 2554, 'n_tokens_neox': 2398, 'n_words': 636}