alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (CryptoSpec.scala)

This example Play Framework source code file (CryptoSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, crypto, cryptospec, fakeapplication, lib, library, none, option, play, play framework, playspecification, secret, some, test

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 examples

Here 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

 

new blog posts

 

Copyright 1998-2021 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.