Skip to content

Instantly share code, notes, and snippets.

@pr1001
Created February 27, 2011 23:47
Show Gist options
  • Save pr1001/846703 to your computer and use it in GitHub Desktop.
Save pr1001/846703 to your computer and use it in GitHub Desktop.
Testing Lift ExtendedSessions
import org.scalatest.fixture.FixtureFlatSpec
import org.scalatest.matchers.ShouldMatchers
import net.liftweb.mapper._
import net.liftweb.common.{Empty, Full, Logger}
import net.liftweb.http.{LiftSession, S}
import net.liftweb.util.StringHelpers
class SessionSpec extends FixtureFlatSpec with ShouldMatchers {
type FixtureParam = User
val session: LiftSession = new LiftSession("", StringHelpers.randomString(20), Empty)
override def withFixture(test: OneArgTest) {
// set up your db here
InMemoryDB.init(User, Session)
try {
// Initialize session state if it is not already
S.initIfUninitted(session) {
// Create the user
val johnDoe = User.create.firstName("John").lastName("Doe").saveMe
test(johnDoe) // Invoke the test function
}
}
finally {
// tear down your db here
InMemoryDB.shutdown
}
}
"Session" should "login a user" in { johnDoe =>
Session.logUserIdIn(johnDoe.userIdAsString)
User.currentUser should equal (Full(johnDoe))
}
it should "create the Session database entry on login" in { johnDoe =>
Session.logUserIdIn(johnDoe.userIdAsString)
val session = Session.find(By(Session.userId, johnDoe.userIdAsString))
session should not equal (Empty)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment