PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/FileFarmer-Core/src/test/scala/ch/filefarmer/tests/repositories/UserRepositoryTests.scala

https://github.com/kufi/Filefarmer
Scala | 87 lines | 67 code | 20 blank | 0 comment | 0 complexity | 1eaf5615c08439e9ac938c273c3f3aae MD5 | raw file
  1. package ch.filefarmer.tests.repositories
  2. import ch.filefarmer.repositories.UserRepository
  3. import com.mongodb.casbah.commons.MongoDBObject
  4. import ch.filefarmer.poso.User
  5. import org.junit.runner.RunWith
  6. import org.scalatest.junit.JUnitRunner
  7. import ch.filefarmer.repositories.DuplicateException
  8. import ch.filefarmer.repositories.ArgumentInvalidException
  9. @RunWith(classOf[JUnitRunner])
  10. class UserRepositoryTests extends BaseRepositoryTestClass {
  11. describe("the user repository") {
  12. it("should add a user") {
  13. val user = new User(userName = "Username", _password = "password")
  14. conn stubs 'connection returning mongoDB
  15. val rep = new UserRepository(conn)
  16. rep.addUser(user)
  17. val q = MongoDBObject("_id" -> user.id)
  18. mongoDB("users").findOne(q) should be('defined)
  19. }
  20. it("should throw an exception if the user already exists") {
  21. val user = new User(userName = "user", _password = "password")
  22. conn stubs 'connection returning mongoDB
  23. val rep = new UserRepository(conn)
  24. rep.addUser(user)
  25. val thrown = evaluating { rep.addUser(user) } should produce[DuplicateException]
  26. thrown.getMessage should equal ("User already exists")
  27. }
  28. it("should throw an argument invalid exception if the user is empty") {
  29. val user = new User(userName = "", _password = "pw")
  30. conn stubs 'connection returning mongoDB
  31. val rep = new UserRepository(conn)
  32. val thrown = evaluating { rep.addUser(user) } should produce[ArgumentInvalidException]
  33. thrown.getMessage should equal ("Username or password is empty")
  34. }
  35. it("should throw an argument invalid exception if the password is empty") {
  36. val user = new User(userName = "user", _password = "")
  37. conn stubs 'connection returning mongoDB
  38. val rep = new UserRepository(conn)
  39. val thrown = evaluating { rep.addUser(user) } should produce[ArgumentInvalidException]
  40. thrown.getMessage should equal ("Username or password is empty")
  41. }
  42. it("should return true if the user and the password match") {
  43. val user = new User(userName = "userCorrect")
  44. user.password = "correct"
  45. conn stubs 'connection returning mongoDB
  46. val rep = new UserRepository(conn)
  47. rep.addUser(user)
  48. rep.validLogin("userCorrect", "correct") should be(true)
  49. }
  50. it("should return false if the user and the password don't match (user wrong)") {
  51. val user = new User(userName = "userFalse")
  52. user.password = "correct"
  53. conn stubs 'connection returning mongoDB
  54. val rep = new UserRepository(conn)
  55. rep.addUser(user)
  56. rep.validLogin("userFalse2", "correct") should be(false)
  57. }
  58. it("should return false if the user and the password don't match (password wrong)") {
  59. val user = new User(userName = "userFalse2")
  60. user.password = "passwordFalse"
  61. conn stubs 'connection returning mongoDB
  62. val rep = new UserRepository(conn)
  63. rep.addUser(user)
  64. rep.validLogin("userFalse2", "passwordFals") should be(false)
  65. }
  66. }
  67. }