|
Play Framework/Scala example source code file (CryptoSpec.scala)
The CryptoSpec.scala Play Framework example source code
/*
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package play.it.libs
import play.api.test._
import play.api.libs.Crypto
import play.api.{PlayException, Mode}
object CryptoSpec extends PlaySpecification {
val Secret = "abcdefghijklmnopqrs"
def fakeApp(m: Mode.Mode, secret: Option[String] = None) = {
new FakeApplication(
additionalConfiguration = secret.map("application.secret" -> _).toMap
) {
override val mode = m
}
}
"Crypto" should {
"secret" should {
"load a configured secret in prod" in new WithApplication(fakeApp(Mode.Prod, Some(Secret))) {
Crypto.secret must_== Secret
}
"load a configured secret in dev" in new WithApplication(fakeApp(Mode.Dev, Some(Secret))) {
Crypto.secret must_== Secret
}
"throw an exception if secret is changeme in prod" in new WithApplication(fakeApp(Mode.Prod, Some("changeme"))) {
Crypto.secret must throwA[PlayException]
}
"throw an exception if no secret in prod" in new WithApplication(fakeApp(Mode.Prod)) {
Crypto.secret must throwA[PlayException]
}
"throw an exception if secret is blank in prod" in new WithApplication(fakeApp(Mode.Prod, Some(" "))) {
Crypto.secret must throwA[PlayException]
}
"throw an exception if secret is empty in prod" in new WithApplication(fakeApp(Mode.Prod, Some(""))) {
Crypto.secret must throwA[PlayException]
}
"generate a secret if secret is changeme in dev" in new WithApplication(fakeApp(Mode.Dev, Some("changeme"))) {
Crypto.secret must_!= "changeme"
}
"generate a secret if no secret in dev" in new WithApplication(fakeApp(Mode.Dev)) {
Crypto.secret must_!= ""
}
"generate a secret if secret is blank in dev" in new WithApplication(fakeApp(Mode.Dev, Some(" "))) {
Crypto.secret must_!= " "
}
"generate a secret if secret is empty in dev" in new WithApplication(fakeApp(Mode.Dev, Some(""))) {
Crypto.secret must_!= ""
}
"generate a stable secret in dev" in new WithApplication(fakeApp(Mode.Dev, Some("changeme"))) {
Crypto.secret must_== Crypto.secret
}
}
}
}
Other Play Framework source code examplesHere is a short list of links related to this Play Framework CryptoSpec.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.