Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

AO3-3848 Added Feature test for a locked user account #3135

Merged
merged 9 commits into from Nov 16, 2017

Conversation

Projects
None yet
4 participants
Contributor

hatal175 commented Nov 1, 2017

Issue

https://otwarchive.atlassian.net/browse/AO3-3848

Purpose

As I could not reproduce the issue, I added a feature test for it.

Testing

It is a feature test.

Credit

Tal Hayon

Please use he.

hatal175 added some commits Nov 1, 2017

features/step_definitions/user_steps.rb
@@ -158,6 +158,15 @@
click_button "Create"
end
+When /^I login username "([^"]*)" and password "([^"]*)" for ([0-9]*) times$/ do |login, password, times|
+ for i in 1..times.to_i do
@houndci-bot

houndci-bot Nov 1, 2017

Prefer each over for.
Useless assignment to variable - i.

@hatal175 hatal175 changed the title from AO3-3848 to AO3-3848 Added Feature test for a locked user account Nov 2, 2017

features/step_definitions/user_steps.rb
@@ -158,6 +158,15 @@
click_button "Create"
end
+When /^I login username "([^"]*)" and password "([^"]*)" for ([0-9]*) times$/ do |login, password, n|
+ n.to_i.times {
@houndci-bot

houndci-bot Nov 2, 2017

Avoid using {...} for multi-line blocks.

sarken approved these changes Nov 12, 2017

Verified that this is working on production, so the test looks good!

Contributor

hatal175 commented Nov 12, 2017

Owner

sarken commented Nov 12, 2017

That's a good point.

It would be easier to do via Cucumber if this were a setting in our config file, which it really should be, and I'd normally suggest making that change. However, we're switching our user authentication to Devise soon and won't have time to merge a code change to Authlogic before then, so it would be somewhat pointless to ask you to do that.

Instead, it might be better to rewrite this as an Rspec test and set the user's failed_login_count to 49, so you only need to make one log in attempt to trigger the error. That way, we're still only changing the tests and can merge this sooner rather than later.

Contributor

hatal175 commented Nov 13, 2017

Owner

sarken commented Nov 13, 2017

If you could change to RSpec now rather than waiting to make the config change, that would actually be really helpful -- it would help us make sure we don't forget to implement account-locking when making the switch.

Contributor

hatal175 commented Nov 13, 2017

+require 'spec_helper'
+
+describe UserSessionsController do
+
@houndci-bot

houndci-bot Nov 14, 2017

Extra empty line detected at block body beginning.

+
+describe UserSessionsController do
+
+ let(:user) { create(:user, activated_at: Time.new(2007,1,1)) }
@houndci-bot

houndci-bot Nov 14, 2017

Space missing after comma.

features/step_definitions/user_steps.rb
@@ -158,6 +158,15 @@
click_button "Create"
end
+When /^I login username "([^"]*)" and password "([^"]*)" for ([0-9]*) times$/ do |login, password, n|
@redsummernight

redsummernight Nov 15, 2017

Contributor

Can you undo changes to this file since the cucumber step is no longer needed?

@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
@redsummernight

redsummernight Nov 15, 2017

Contributor

You should use double quotes here for consistency.

+ context "after failed to login more than consecutive login limit" do
+ before do
+ allow(UserSession).to receive(:consecutive_failed_logins_limit).and_return(2)
+ 3.times do
@redsummernight

redsummernight Nov 15, 2017

Contributor

Since you mocked consecutive_failed_logins_limit as 2, 2.times here is enough for the next post to fail. Can you make "2" a local variable?

+ before do
+ allow(UserSession).to receive(:consecutive_failed_logins_limit).and_return(2)
+ 3.times do
+ post :create, params: { user_session: { login: user.login, password: "aaa", remember_me: true } }
@redsummernight

redsummernight Nov 15, 2017

Contributor

You can leave out remember_me: true from all the posts, it's not needed for the test.

+ end
+ end
+
+ context "when trying to login" do
@redsummernight

redsummernight Nov 15, 2017

Contributor

You don't need this nested context since there's only one test, I'd move the last post into the it block.

+ end
+
+ it "should not succeed because user is locked" do
+ expect(flash.now[:error]).to match("Your account has been locked")
@redsummernight

redsummernight Nov 15, 2017

Contributor

You should use flash[:error] to be consistent with the other tests.

+ before do
+ max_consecutive_attempts = 2
+ allow(UserSession).to receive(:consecutive_failed_logins_limit).and_return(max_consecutive_attempts)
+ (max_consecutive_attempts + 1).times do
@redsummernight

redsummernight Nov 16, 2017

Contributor

You don't need to add 1, this test works if you use max_consecutive_attempts.times.

+ before do
+ max_consecutive_attempts = 2
+ allow(UserSession).to receive(:consecutive_failed_logins_limit).and_return(max_consecutive_attempts)
+ (max_consecutive_attempts).times do
@houndci-bot

houndci-bot Nov 16, 2017

Don't use parentheses around a variable.

Contributor

redsummernight commented Nov 16, 2017

Looks good to me! 👍

@sarken sarken merged commit 761ab98 into otwcode:master Nov 16, 2017

3 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
hound No violations found. Woof!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment