test_auth_login.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. from flask import url_for
  2. from app.extensions import db
  3. from app.models import User, AccountActivation
  4. def test_auth_login_success_mfa_disabled(flask_client):
  5. User.create(email="a@b.c", password="password", name="Test User", activated=True)
  6. db.session.commit()
  7. r = flask_client.post(
  8. url_for("api.auth_login"),
  9. json={"email": "a@b.c", "password": "password", "device": "Test Device"},
  10. )
  11. assert r.status_code == 200
  12. assert r.json["api_key"]
  13. assert r.json["mfa_enabled"] == False
  14. assert r.json["mfa_key"] is None
  15. assert r.json["name"] == "Test User"
  16. def test_auth_login_success_mfa_enabled(flask_client):
  17. User.create(
  18. email="a@b.c",
  19. password="password",
  20. name="Test User",
  21. activated=True,
  22. enable_otp=True,
  23. )
  24. db.session.commit()
  25. r = flask_client.post(
  26. url_for("api.auth_login"),
  27. json={"email": "a@b.c", "password": "password", "device": "Test Device"},
  28. )
  29. assert r.status_code == 200
  30. assert r.json["api_key"] is None
  31. assert r.json["mfa_enabled"] == True
  32. assert r.json["mfa_key"]
  33. assert r.json["name"] == "Test User"
  34. def test_auth_login_device_exist(flask_client):
  35. User.create(email="a@b.c", password="password", name="Test User", activated=True)
  36. db.session.commit()
  37. r = flask_client.post(
  38. url_for("api.auth_login"),
  39. json={"email": "a@b.c", "password": "password", "device": "Test Device"},
  40. )
  41. assert r.status_code == 200
  42. api_key = r.json["api_key"]
  43. assert r.json["mfa_enabled"] == False
  44. assert r.json["mfa_key"] is None
  45. assert r.json["name"] == "Test User"
  46. # same device, should return same api_key
  47. r = flask_client.post(
  48. url_for("api.auth_login"),
  49. json={"email": "a@b.c", "password": "password", "device": "Test Device"},
  50. )
  51. assert r.json["api_key"] == api_key
  52. def test_auth_register_success(flask_client):
  53. assert AccountActivation.get(1) is None
  54. r = flask_client.post(
  55. url_for("api.auth_register"), json={"email": "a@b.c", "password": "password"}
  56. )
  57. assert r.status_code == 200
  58. assert r.json["msg"]
  59. # make sure an activation code is created
  60. act_code = AccountActivation.get(1)
  61. assert act_code
  62. assert len(act_code.code) == 6
  63. assert act_code.tries == 3
  64. def test_auth_register_too_short_password(flask_client):
  65. r = flask_client.post(
  66. url_for("api.auth_register"), json={"email": "a@b.c", "password": "short"}
  67. )
  68. assert r.status_code == 400
  69. assert r.json["error"] == "password too short"
  70. def test_auth_activate_success(flask_client):
  71. r = flask_client.post(
  72. url_for("api.auth_register"), json={"email": "a@b.c", "password": "password"}
  73. )
  74. assert r.status_code == 200
  75. assert r.json["msg"]
  76. # get the activation code
  77. act_code = AccountActivation.get(1)
  78. assert act_code
  79. assert len(act_code.code) == 6
  80. r = flask_client.post(
  81. url_for("api.auth_activate"), json={"email": "a@b.c", "code": act_code.code}
  82. )
  83. assert r.status_code == 200
  84. def test_auth_activate_wrong_email(flask_client):
  85. r = flask_client.post(
  86. url_for("api.auth_activate"), json={"email": "a@b.c", "code": "123456"}
  87. )
  88. assert r.status_code == 400
  89. def test_auth_activate_user_already_activated(flask_client):
  90. User.create(email="a@b.c", password="password", name="Test User", activated=True)
  91. db.session.commit()
  92. r = flask_client.post(
  93. url_for("api.auth_activate"), json={"email": "a@b.c", "code": "123456"}
  94. )
  95. assert r.status_code == 400
  96. def test_auth_activate_wrong_code(flask_client):
  97. r = flask_client.post(
  98. url_for("api.auth_register"), json={"email": "a@b.c", "password": "password"}
  99. )
  100. assert r.status_code == 200
  101. assert r.json["msg"]
  102. # get the activation code
  103. act_code = AccountActivation.get(1)
  104. assert act_code
  105. assert len(act_code.code) == 6
  106. assert act_code.tries == 3
  107. # make sure to create a wrong code
  108. wrong_code = act_code.code + "123"
  109. r = flask_client.post(
  110. url_for("api.auth_activate"), json={"email": "a@b.c", "code": wrong_code}
  111. )
  112. assert r.status_code == 400
  113. # make sure the nb tries decrements
  114. act_code = AccountActivation.get(1)
  115. assert act_code.tries == 2
  116. def test_auth_activate_too_many_wrong_code(flask_client):
  117. r = flask_client.post(
  118. url_for("api.auth_register"), json={"email": "a@b.c", "password": "password"}
  119. )
  120. assert r.status_code == 200
  121. assert r.json["msg"]
  122. # get the activation code
  123. act_code = AccountActivation.get(1)
  124. assert act_code
  125. assert len(act_code.code) == 6
  126. assert act_code.tries == 3
  127. # make sure to create a wrong code
  128. wrong_code = act_code.code + "123"
  129. for _ in range(2):
  130. r = flask_client.post(
  131. url_for("api.auth_activate"), json={"email": "a@b.c", "code": wrong_code}
  132. )
  133. assert r.status_code == 400
  134. # the activation code is deleted
  135. r = flask_client.post(
  136. url_for("api.auth_activate"), json={"email": "a@b.c", "code": wrong_code}
  137. )
  138. assert r.status_code == 410
  139. # make sure the nb tries decrements
  140. assert AccountActivation.get(1) is None
  141. def test_auth_reactivate_success(flask_client):
  142. User.create(email="a@b.c", password="password", name="Test User")
  143. db.session.commit()
  144. r = flask_client.post(url_for("api.auth_reactivate"), json={"email": "a@b.c"})
  145. assert r.status_code == 200
  146. # make sure an activation code is created
  147. act_code = AccountActivation.get(1)
  148. assert act_code
  149. assert len(act_code.code) == 6
  150. assert act_code.tries == 3