<commit_before>package repository

import play.api.test.PlaySpecification
import reactivemongo.api.indexes.IndexType
import securesocial.core.authenticator.{CookieAuthenticator, HttpHeaderAuthenticator, IdGenerator}
import securesocial.core.services.SaveMode
import security.{ProfileCookieAuthenticatorStore, ProfileHttpHeaderAuthenticatorStore}
import time.DateTimeUtils._
import utils.StartedFakeApplication

class AuthenticatorStoreSpec extends PlaySpecification with StartedFakeApplication {

  "Authenticator store" should {

    "find a cookie authenticator" in new AuthenticatorTestCase {
      val user = await(cookieStore.save(profile, SaveMode.SignUp))
      val created = now
      val expire = created.plusHours(1)
      val lastUsed = created.plusMillis(1)
      await(cookieStore.save(CookieAuthenticator(authenticatorId, user, expire, lastUsed, created, cookieStore), 3600))

      val authenticator = await(cookieStore.find(authenticatorId)).get

      authenticator.user must be equalTo user
      authenticator.expirationDate must be equalTo expire
      authenticator.lastUsed must be equalTo lastUsed
      authenticator.creationDate must be equalTo created
    }

    "delete a cookie authenticator" in new AuthenticatorTestCase {
      val user = await(cookieStore.save(profile, SaveMode.SignUp))
      val created = now
      val expire = created.plusHours(1)
      val lastUsed = created.plusMillis(1)
      await(cookieStore.save(CookieAuthenticator(authenticatorId, user, expire, lastUsed, created, cookieStore), 3600))

      await(cookieStore.delete(authenticatorId))

      await(cookieStore.find(authenticatorId)) must be equalTo None
    }

    "not find an expired cookie authenticator" in new AuthenticatorTestCase {
      val user = await(cookieStore.save(profile, SaveMode.SignUp))
      val created = now.minusHours(1)
      val expire = created.plusHours(1)
      val lastUsed = created
      await(cookieStore.save(CookieAuthenticator(authenticatorId, user, expire, lastUsed, created, cookieStore), 3600))

      val authenticator = await(cookieStore.find(authenticatorId))

      await(cookieStore.find(authenticatorId)) must be equalTo None
    }

    "have an index on authenticator id" in new AuthenticatorTestCase {
      val indexes = await(cookieStore.indexes())
      indexes.filter(_.key == Seq("authenticator.id" -> IndexType.Ascending)) must not be empty
    }

    "find a http header authenticator" in new AuthenticatorTestCase {
      val user = await(httpHeaderStore.save(profile, SaveMode.SignUp))
      val created = now
      val expire = created.plusHours(1)
      val lastUsed = created.plusMillis(1)
      await(httpHeaderStore.save(HttpHeaderAuthenticator(authenticatorId, user, expire, lastUsed, created, httpHeaderStore), 3600))

      val authenticator = await(httpHeaderStore.find(authenticatorId)).get

      authenticator.user must be equalTo user
      authenticator.expirationDate must be equalTo expire
      authenticator.lastUsed must be equalTo lastUsed
      authenticator.creationDate must be equalTo created
    }

    "delete a HTTP authenticator" in new AuthenticatorTestCase {
      val user = await(httpHeaderStore.save(profile, SaveMode.SignUp))
      val created = now
      val expire = created.plusHours(1)
      val lastUsed = created.plusMillis(1)
      await(httpHeaderStore.save(HttpHeaderAuthenticator(authenticatorId, user, expire, lastUsed, created, httpHeaderStore), 3600))

      await(httpHeaderStore.delete(authenticatorId))

      await(httpHeaderStore.find(authenticatorId)) must be equalTo None
    }

    "not find an expired HTTP authenticator" in new AuthenticatorTestCase {
      val user = await(httpHeaderStore.save(profile, SaveMode.SignUp))
      val created = now.minusHours(1)
      val expire = created.plusHours(1)
      val lastUsed = created
      await(httpHeaderStore.save(HttpHeaderAuthenticator(authenticatorId, user, expire, lastUsed, created, httpHeaderStore), 3600))

      val authenticator = await(httpHeaderStore.find(authenticatorId))

      await(httpHeaderStore.find(authenticatorId)) must be equalTo None
    }
  }

  trait AuthenticatorTestCase extends ProfileTestCase {
    val authenticatorId = await(new IdGenerator.Default().generate)
    val cookieStore = new ProfileCookieAuthenticatorStore
    val httpHeaderStore = new ProfileHttpHeaderAuthenticatorStore
  }
}
<commit_msg>Improve readability of authenticator integration tests.<commit_after>package repository

import play.api.test.PlaySpecification
import reactivemongo.api.indexes.IndexType
import securesocial.core.authenticator.{CookieAuthenticator, HttpHeaderAuthenticator, IdGenerator}
import securesocial.core.services.SaveMode
import security.{ProfileCookieAuthenticatorStore, ProfileHttpHeaderAuthenticatorStore}
import time.DateTimeUtils._
import utils.StartedFakeApplication

class AuthenticatorStoreSpec extends PlaySpecification with StartedFakeApplication {

  "Authenticator store" should {

    "find a valid unexpired cookie authenticator" in new AuthenticatorTestCase {
      val justCreated = now
      val notExpired = justCreated.plusHours(1)
      val justUsed = justCreated
      await(cookieStore.save(CookieAuthenticator(authenticatorId, user, notExpired, justUsed, justCreated, cookieStore), 3600))

      val authenticator = await(cookieStore.find(authenticatorId)).get

      authenticator.user must be equalTo user
      authenticator.expirationDate must be equalTo notExpired
      authenticator.lastUsed must be equalTo justUsed
      authenticator.creationDate must be equalTo justCreated
    }

    "delete a cookie authenticator" in new AuthenticatorTestCase {
      val justCreated = now
      val notExpired = justCreated.plusHours(1)
      val justUsed = justCreated
      await(cookieStore.save(CookieAuthenticator(authenticatorId, user, notExpired, justUsed, justCreated, cookieStore), 3600))

      await(cookieStore.delete(authenticatorId))

      await(cookieStore.find(authenticatorId)) must be equalTo None
    }

    "not find an expired cookie authenticator" in new AuthenticatorTestCase {
      val anHourOld = now.minusHours(1)
      val justExpired = now.minusMillis(1)
      val usedAtCreation = anHourOld
      await(cookieStore.save(CookieAuthenticator(authenticatorId, user, justExpired, usedAtCreation, anHourOld, cookieStore), 3600))

      val authenticator = await(cookieStore.find(authenticatorId))

      await(cookieStore.find(authenticatorId)) must be equalTo None
    }

    "have an index on authenticator id" in new AuthenticatorTestCase {
      val indexes = await(cookieStore.indexes())
      indexes.filter(_.key == Seq("authenticator.id" -> IndexType.Ascending)) must not be empty
    }

    "find a valid unexpired http header authenticator" in new AuthenticatorTestCase {
      val justCreated = now
      val notExpired = justCreated.plusHours(1)
      val justUsed = justCreated
      await(httpHeaderStore.save(HttpHeaderAuthenticator(authenticatorId, user, notExpired, justUsed, justCreated, httpHeaderStore), 3600))

      val authenticator = await(httpHeaderStore.find(authenticatorId)).get

      authenticator.user must be equalTo user
      authenticator.expirationDate must be equalTo notExpired
      authenticator.lastUsed must be equalTo justUsed
      authenticator.creationDate must be equalTo justCreated
    }

    "delete a HTTP authenticator" in new AuthenticatorTestCase {
      val justCreated = now
      val notExpired = justCreated.plusHours(1)
      val justUsed = justCreated
      await(httpHeaderStore.save(HttpHeaderAuthenticator(authenticatorId, user, notExpired, justUsed, justCreated, httpHeaderStore), 3600))

      await(httpHeaderStore.delete(authenticatorId))

      await(httpHeaderStore.find(authenticatorId)) must be equalTo None
    }

    "not find an expired HTTP authenticator" in new AuthenticatorTestCase {
      val anHourOld = now.minusHours(1)
      val justExpired = now.minusMillis(1)
      val usedAtCreation = anHourOld
      await(httpHeaderStore.save(HttpHeaderAuthenticator(authenticatorId, user, justExpired, usedAtCreation, anHourOld, httpHeaderStore), 3600))

      val authenticator = await(httpHeaderStore.find(authenticatorId))

      await(httpHeaderStore.find(authenticatorId)) must be equalTo None
    }
  }

  trait AuthenticatorTestCase extends ProfileTestCase {
    val authenticatorId = await(new IdGenerator.Default().generate)
    val cookieStore = new ProfileCookieAuthenticatorStore
    val httpHeaderStore = new ProfileHttpHeaderAuthenticatorStore
    val user = await(cookieStore.save(profile, SaveMode.SignUp))
  }
}

{'fraction_non_alphanumeric': 0.0715855209899387, 'fraction_numerical': 0.007863999074823639, 'mean_word_length': 4.398252184769039, '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': '3821188', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2345, 'n_words': 597}