require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest
  test "signup fails with no name" do
    get signup_path
    assert_no_difference 'User.count' do
      post signup_path, params: { user: { name: '',
                                         email: 'user@invalid',
                                         password: 'a',
                                         password_confirmation: 'b',
                                       }
                               }
    end
    assert_template 'users/new'
    assert_select 'div#error_explanation'
    assert_select 'div.field_with_errors'
  end
  
  test "happy signup" do
    delete logout_path
    get signup_path
    assert_difference 'User.count', 1 do
      pwd = 'a' * 12
      post users_path, params: { user: { name: 'ferd',
                                          email: 'user@valid.com',
                                          password: pwd,
                                          password_confirmation: pwd,
                                        }
                                }
    end
    assert_equal 1, ActionMailer::Base.deliveries.size
    user = assigns(:user)
    assert_not user.activated?
    # Try to log in before activation
    log_in_as(user)
    assert_not is_logged_in?
    # Invalid activation token
    get edit_account_activation_path('splibitch', email: user.email)
    assert_not is_logged_in?

    # Valid token, wrong email
    get edit_account_activation_path(user.activation_token, email: user.email + "X")
    assert_not is_logged_in?

    # Valid activation token
    get edit_account_activation_path(user.activation_token, email: user.email)
    assert user.reload.activated?
    follow_redirect!
    assert_template 'users/show'

    assert is_logged_in?
  end
end
