/test/integration/event_log_creation_test.rb

https://github.com/alphagov/signon · Ruby · 222 lines · 193 code · 29 blank · 0 comment · 2 complexity · c245586548eb9cce38d1de719f2ade9f MD5 · raw file

  1. require "test_helper"
  2. require "support/password_helpers"
  3. class EventLogCreationIntegrationTest < ActionDispatch::IntegrationTest
  4. include PasswordHelpers
  5. setup do
  6. @admin = create(:admin_user, name: "Admin User")
  7. @user = create(:user, name: "Normal User")
  8. end
  9. test "record successful login" do
  10. visit root_path
  11. signin_with(@user)
  12. assert_equal 1, @user.event_logs.count
  13. assert_equal EventLog::SUCCESSFUL_LOGIN, @user.event_logs.last.entry
  14. end
  15. context "recording unsuccessful login" do
  16. should "record unsuccessful login for a valid email" do
  17. visit root_path
  18. signin_with(email: @user.email, password: :incorrect)
  19. assert_equal 1, @user.event_logs.count
  20. assert_equal EventLog::UNSUCCESSFUL_LOGIN, @user.event_logs.last.entry
  21. end
  22. should "record an invalid email" do
  23. visit root_path
  24. signin_with(email: "nonexistent@example.com", password: "anything")
  25. assert_equal 1, EventLog.count
  26. assert_equal EventLog::NO_SUCH_ACCOUNT_LOGIN, EventLog.last.entry
  27. end
  28. should "raise an error when missing CSRF token" do
  29. assert_raises ActionController::InvalidAuthenticityToken do
  30. post "/users/sign_in", params: { "user" => { "email" => { "foo" => "bar" }, :password => "anything" } }
  31. end
  32. end
  33. end
  34. test "record password reset request" do
  35. visit root_path
  36. click_on "Forgot your password?"
  37. fill_in "Email address", with: @user.email
  38. click_on "Send email"
  39. assert_equal EventLog::PASSWORD_RESET_REQUEST, @user.event_logs.first.entry
  40. end
  41. test "record password reset page requested" do
  42. token_received_in_email = @user.send_reset_password_instructions
  43. visit edit_user_password_path(reset_password_token: token_received_in_email)
  44. assert_equal EventLog::PASSWORD_RESET_LOADED, @user.event_logs.first.entry
  45. end
  46. test "record password reset page loaded but token expired" do
  47. token_received_in_email = Timecop.freeze((User.reset_password_within + 1.hour).ago) do
  48. @user.send_reset_password_instructions
  49. end
  50. visit edit_user_password_path(reset_password_token: token_received_in_email)
  51. assert_equal EventLog::PASSWORD_RESET_LOADED_BUT_TOKEN_EXPIRED, @user.event_logs.first.entry
  52. end
  53. test "record password reset failed" do
  54. token_received_in_email = @user.send_reset_password_instructions
  55. visit edit_user_password_path(reset_password_token: token_received_in_email)
  56. click_on "Save password"
  57. event_log = @user.event_logs.first
  58. assert_equal EventLog::PASSWORD_RESET_FAILURE, event_log.entry
  59. assert_match "Password can't be blank", event_log.trailing_message
  60. end
  61. test "record successful password reset from email" do
  62. token_received_in_email = @user.send_reset_password_instructions
  63. visit edit_user_password_path(reset_password_token: token_received_in_email)
  64. new_password = "diagram donkey doodle"
  65. fill_in "New password", with: new_password
  66. fill_in "Confirm new password", with: new_password
  67. click_on "Save password"
  68. assert_includes @user.event_logs.map(&:entry), EventLog::SUCCESSFUL_PASSWORD_RESET
  69. end
  70. test "record successful password change" do
  71. new_password = "correct horse battery daffodil"
  72. visit root_path
  73. signin_with(@user)
  74. change_password(
  75. old: @user.password,
  76. new: new_password,
  77. new_confirmation: new_password,
  78. )
  79. # multiple events are registered with the same time, order changes.
  80. assert_includes @user.event_logs.map(&:entry), EventLog::SUCCESSFUL_PASSWORD_CHANGE
  81. end
  82. test "record unsuccessful password change" do
  83. visit root_path
  84. signin_with(@user)
  85. change_password(
  86. old: @user.password,
  87. new: @user.password,
  88. new_confirmation: @user.password,
  89. )
  90. # multiple events are registered with the same time, order changes.
  91. assert_includes @user.event_logs.map(&:entry), EventLog::UNSUCCESSFUL_PASSWORD_CHANGE
  92. end
  93. test "record account locked if password entered too many times" do
  94. visit root_path
  95. 7.times { signin_with(email: @user.email, password: :incorrect) }
  96. # multiple events are registered with the same time, order changes.
  97. assert_includes @user.event_logs.map(&:entry), EventLog::ACCOUNT_LOCKED
  98. end
  99. test "record account unlocked along with event initiator" do
  100. @user.lock_access!
  101. visit root_path
  102. signin_with(@admin)
  103. first_letter_of_name = @user.name[0]
  104. visit users_path(letter: first_letter_of_name)
  105. click_on "Unlock"
  106. visit event_logs_user_path(@user)
  107. assert page.has_content?(EventLog::MANUAL_ACCOUNT_UNLOCK.description + " by " + @admin.name)
  108. end
  109. test "record user suspension along with event initiator" do
  110. visit root_path
  111. signin_with(@admin)
  112. first_letter_of_name = @user.name[0]
  113. visit users_path(letter: first_letter_of_name)
  114. click_on @user.name.to_s
  115. click_on "Suspend user"
  116. check "Suspended?"
  117. fill_in "Reason for suspension", with: "Assaulting superior officer"
  118. click_on "Save"
  119. visit event_logs_user_path(@user)
  120. assert page.has_content?(EventLog::ACCOUNT_SUSPENDED.description + " by " + @admin.name)
  121. end
  122. test "record suspended user's attempt to login with correct credentials" do
  123. @user.suspend("Assaulting superior officer")
  124. visit root_path
  125. signin_with(@user)
  126. assert_equal @user.event_logs.last.entry, EventLog::SUSPENDED_ACCOUNT_AUTHENTICATED_LOGIN
  127. end
  128. test "record user unsuspension along with event initiator" do
  129. @user.suspend("Gross negligence")
  130. visit root_path
  131. signin_with(@admin)
  132. first_letter_of_name = @user.name[0]
  133. visit users_path(letter: first_letter_of_name)
  134. click_on @user.name.to_s
  135. click_on "Unsuspend user"
  136. uncheck "Suspended?"
  137. click_on "Save"
  138. visit event_logs_user_path(@user)
  139. assert page.has_content?(EventLog::ACCOUNT_UNSUSPENDED.description + " by " + @admin.name)
  140. end
  141. context "recording user's ip address" do
  142. should "record user's IPv4 address for successful login" do
  143. page.driver.options[:headers] = { "REMOTE_ADDR" => "1.2.3.4" }
  144. visit root_path
  145. signin_with(@user)
  146. ip_address = @user.event_logs.first.ip_address_string
  147. assert_equal "1.2.3.4", ip_address
  148. end
  149. should "record user's IPv4 address for unsuccessful login" do
  150. page.driver.options[:headers] = { "REMOTE_ADDR" => "4.5.6.7" }
  151. visit root_path
  152. signin_with(email: @user.email, password: :incorrect)
  153. ip_address = @user.event_logs.last.ip_address_string
  154. assert_equal "4.5.6.7", ip_address
  155. end
  156. should "record user's IPv6 address" do
  157. page.driver.options[:headers] = { "REMOTE_ADDR" => "2001:0db8:0000:0000:0008:0800:200c:417a" }
  158. visit root_path
  159. signin_with(@user)
  160. ip_address = @user.event_logs.first.ip_address_string
  161. assert_equal "2001:db8::8:800:200c:417a", ip_address
  162. end
  163. end
  164. test "record who the account was created by" do
  165. visit root_path
  166. signin_with(@admin)
  167. visit users_path
  168. click_on "Create user"
  169. fill_in "Name", with: "New User"
  170. fill_in "Email", with: "test@test.com"
  171. click_on "Create user and send email"
  172. event_log = User.last.event_logs.first
  173. assert_equal @admin, event_log.initiator
  174. assert_equal EventLog::ACCOUNT_INVITED, event_log.entry
  175. end
  176. end