daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Jan 28th, 2016 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import controllers.{UserController, Session}
  2. import database.Database
  3. import models.User
  4. import org.scalatest.BeforeAndAfter
  5. import org.scalatestplus.play.PlaySpec
  6. import play.api.http.MimeTypes
  7. import play.api.libs.json.Json
  8. import play.api.test.Helpers._
  9. import play.api.test._
  10. /**
  11.   * Created by skripnik_ma on 25.01.16.
  12.   */
  13. class UserControllerSpec extends PlaySpec with BeforeAndAfter {
  14.  
  15.   before {
  16.     Database.init
  17.   }
  18.  
  19.   after {
  20.     Database.dropAllRecords
  21.   }
  22.  
  23.   val userLogin = "dumbdude13"
  24.   val userMail = "reserved@gmail.com"
  25.   val userPassword = "iamactuallyfromgreece"
  26.  
  27.   val app = FakeApplication()
  28.   val controller = new UserController
  29.  
  30.   "register method" should {
  31.     "return json with user id" in {
  32.  
  33.       val user = Json.obj(("login", userLogin), ("email", userMail), ("password", userPassword))
  34.  
  35.       running(app) {
  36.         val request = FakeRequest(Helpers.POST,
  37.           controllers.routes.UserController.register.url,
  38.           FakeHeaders(),
  39.           user)
  40.  
  41.         val result = controller.register.apply(request)
  42.  
  43.         assert(status(result) == OK)
  44.         assert(contentType(result).contains(MimeTypes.JSON))
  45.         assert(contentAsJson(result).toString().matches("\\{\"id\":\"#\\d+:\\d+\"\\}"))
  46.       }
  47.     }
  48.  
  49.     "return json with userRegistrationError" in {
  50.  
  51.       val user = Json.obj(("login", userLogin), ("email", userMail), ("password", userPassword))
  52.       running(app) {
  53.         val request = FakeRequest(Helpers.POST,
  54.           controllers.routes.UserController.register.url,
  55.           FakeHeaders(),
  56.           user)
  57.  
  58.         controller.register.apply(request)
  59.         val result = controller.register.apply(request)
  60.  
  61.         assert(status(result) == BAD_REQUEST)
  62.         assert(contentType(result).contains(MimeTypes.JSON))
  63.         assert(contentAsJson(result) == Json.obj(
  64.           "errors" -> Json.arr(
  65.             Json.obj("data" -> "null"),
  66.             Json.obj("message" -> "User with this login or email already exists"),
  67.             Json.obj("code" -> "userRegistrationError")
  68.           )))
  69.       }
  70.     }
  71.  
  72.     "return json with jsonFormatError message" in {
  73.       val user = Json.obj(("w", userLogin), ("email", userMail), ("password", userPassword))
  74.  
  75.       running(app) {
  76.         val request = FakeRequest(Helpers.POST,
  77.           controllers.routes.UserController.register.url,
  78.           FakeHeaders(),
  79.           user)
  80.  
  81.         val result = controller.register.apply(request)
  82.  
  83.         assert(status(result) == BAD_REQUEST)
  84.         assert(contentType(result).contains(MimeTypes.JSON))
  85.       }
  86.     }
  87.   }
  88.  
  89.   "login method" should {
  90.     "log in user and start valid session and return it's token in the json object" in {
  91.  
  92.       val olduser = Json.obj(("login", userLogin), ("email", userMail), ("password", userPassword))
  93.       val user = Json.obj(("username", userMail), ("password", userPassword))
  94.  
  95.       running(app) {
  96.         val registerRequest = FakeRequest(Helpers.POST,
  97.           controllers.routes.UserController.register.url,
  98.           FakeHeaders(),
  99.           olduser)
  100.         controller.register.apply(registerRequest)
  101.  
  102.         val loginRequest = FakeRequest(Helpers.POST,
  103.           controllers.routes.UserController.login.url,
  104.           FakeHeaders(),
  105.           user)
  106.         val result = controller.login.apply(loginRequest)
  107.         val logeduser = User.getByLogin(userLogin)
  108.  
  109.         assert(status(result) == OK)
  110.         assert(contentType(result).contains(MimeTypes.JSON))
  111.         assert(contentAsJson(result).toString.matches("""\{"token":"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}","id":"#\d+:\d+"\}"""))
  112.         assert(logeduser == Session.valid((contentAsJson(result) \ "token").get.toString.replaceAll("\"", "")).get)
  113.       }
  114.     }
  115.  
  116.     "return json with authentification error" in {
  117.  
  118.       val olduser = Json.obj(("login", userLogin), ("email", userMail), ("password", userPassword))
  119.       val baduser = Json.obj(("username", userMail), ("password", "iamfromfrance"))
  120.  
  121.       running(app) {
  122.         val registerRequest = FakeRequest(Helpers.POST,
  123.           controllers.routes.UserController.register.url,
  124.           FakeHeaders(),
  125.           olduser)
  126.         controller.register.apply(registerRequest)
  127.       }
  128.  
  129.       val loginRequest = FakeRequest(Helpers.POST,
  130.         controllers.routes.UserController.login.url,
  131.         FakeHeaders(),
  132.         baduser)
  133.       val result = controller.login.apply(loginRequest)
  134.  
  135.       assert(status(result) == BAD_REQUEST)
  136.       assert(contentType(result).contains(MimeTypes.JSON))
  137.       assert(contentAsJson(result) == Json.obj(
  138.         "errors" -> Json.arr(
  139.           Json.obj("data" -> "null"),
  140.           Json.obj("message" -> "Login or password is incorrect"),
  141.           Json.obj("code" -> "authenticationError")
  142.         )))
  143.     }
  144.   }
  145.  
  146.   "logout method" should {
  147.     "log user out" in {
  148.  
  149.       val olduser = Json.obj(("login", userLogin), ("email", userMail), ("password", userPassword))
  150.       val user = Json.obj(("username", userMail), ("password", userPassword))
  151.  
  152.       running(app) {
  153.         val registerRequest = FakeRequest(Helpers.POST,
  154.           controllers.routes.UserController.register.url,
  155.           FakeHeaders(),
  156.           olduser)
  157.           controller.register.apply(registerRequest)
  158.  
  159.         val loginRequest = FakeRequest(Helpers.POST,
  160.           controllers.routes.UserController.login.url,
  161.           FakeHeaders(),
  162.           user)
  163.  
  164.         val loginResult = controller.login.apply(loginRequest)
  165.         val correctToken = (contentAsJson(loginResult) \ "token").get.toString.replaceAll("\"", "")
  166.  
  167.         val logoutRequest = FakeRequest(Helpers.GET,
  168.         "/auth/logout",
  169.           FakeHeaders(Seq(("Token", correctToken))),
  170.           "")
  171.         val result = route(logoutRequest).get
  172.  
  173.         assert(status(result) == OK)
  174.       }
  175.     }
  176.   }
  177. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top