# encoding: utf-8
require 'browser_test_helper'

class SignupPasswordChangeAndResetTest < TestCase
  def test_signup
    signup_user_email = "signup-test-#{rand(999_999)}@example.com"
    @browser = browser_instance
    location(url: browser_url)
    click(css: 'a[href="#signup"]')
    exists(css: '.signup')

    # signup
    set(
      css: 'input[name="firstname"]',
      value: 'Signup Firstname',
    )
    set(
      css: 'input[name="lastname"]',
      value: 'Signup Lastname',
    )
    set(
      css: 'input[name="email"]',
      value: signup_user_email,
    )
    set(
      css: 'input[name="password"]',
      value: 'some-pass',
    )
    set(
      css: 'input[name="password_confirm"]',
      value: 'some-pass',
    )
    click(css: 'button.js-submit')
    sleep 5

    exists_not(css: '.signup')

    match(
      css: '.user-menu .user a',
      value: signup_user_email,
      attribute: 'title',
    )

    # check email verify
    location(url: "#{browser_url}#email_verify/not_existing")
    watch_for(
      css: '#content',
      value: 'Unable to verify email',
    )
    logout()

    login(
      username: signup_user_email,
      password: 'some-pass',
      url: "#{browser_url}#email_verify/not_existing2",
    )
    watch_for(
      css: '#content',
      value: 'Unable to verify email',
    )
    execute(
      js: 'App.Event.trigger("user_signup_verify", App.Session.get())',
    )
    watch_for(
      css: '.modal',
      value: 'Account not verified',
    )
    click(css: '.modal .js-submit')
    execute(
      js: 'App.Auth.logout()',
    )
    sleep 6
    watch_for(
      css: '#login',
    )
    login(
      username: signup_user_email,
      password: 'some-pass',
    )
    watch_for(
      css: '#content',
      value: 'Your email address has been verified',
    )
    exists_not(
      css: '.modal',
    )
    sleep 2

    # change password
    click(css: '.navbar-items-personal .user a')
    sleep 1
    click(css: 'a[href="#profile"]')
    click(css: 'a[href="#profile/password"]')
    set(
      css: 'input[name="password_old"]',
      value: 'nonexisiting',
    )
    set(
      css: 'input[name="password_new"]',
      value: 'some',
    )
    set(
      css: 'input[name="password_new_confirm"]',
      value: 'some',
    )
    click(css: '.content .btn--primary')

    watch_for(
      css: 'body',
      value: 'current password is wrong',
    )

    set(
      css: 'input[name="password_old"]',
      value: 'some-pass',
    )
    set(
      css: 'input[name="password_new_confirm"]',
      value: 'some2',
    )
    click(css: '.content .btn--primary')
    watch_for(
      css: 'body',
      value: 'passwords do not match',
    )

    set(
      css: 'input[name="password_new"]',
      value: 'some',
    )
    set(
      css: 'input[name="password_new_confirm"]',
      value: 'some',
    )
    click(css: '.content .btn--primary')

    watch_for(
      css: 'body',
      value: 'it must be at least',
    )

    set(
      css: 'input[name="password_new"]',
      value: 'some-pass-new',
    )
    set(
      css: 'input[name="password_new_confirm"]',
      value: 'some-pass-new',
    )
    click(css: '.content .btn--primary')

    watch_for(
      css: 'body',
      value: 'must contain at least 1 digit',
    )

    set(
      css: 'input[name="password_new"]',
      value: 'some-pass-new2',
    )
    set(
      css: 'input[name="password_new_confirm"]',
      value: 'some-pass-new2',
    )
    click(css: '.content .btn--primary')

    watch_for(
      css: 'body',
      value: 'Password changed successfully',
    )
    logout()

    # check login with new pw
    login(
      username: signup_user_email,
      password: 'some-pass-new2',
    )
    logout()

    # reset password (not possible)
    location(url: browser_url + '/#password_reset_verify/not_existing_token')

    watch_for(
      css: 'body',
      value: 'Token is invalid',
    )

    # reset password (with valid session - should not be possible)
    login(
      username: signup_user_email,
      password: 'some-pass-new2',
      url: browser_url,
    )

    location(url: browser_url + '/#password_reset')
    sleep 1

    match_not(
      css: 'body',
      value: 'password',
    )
    logout()

    # reset password (correct way)
    click(css: 'a[href="#password_reset"]')

    set(
      css: 'input[name="username"]',
      value: 'nonexisiting',
    )
    click(css: '.content .btn--primary')
    watch_for(
      css: 'body',
      value: 'address invalid',
    )

    set(
      css: 'input[name="username"]',
      value: signup_user_email,
    )
    click(css: '.content .btn--primary')
    watch_for(
      css: 'body',
      value: 'sent password reset instructions',
    )

    # redirect to "#password_reset_verify/#{token}" url by app, because of "developer_mode"
    watch_for(
      css: 'body',
      value: 'Choose your new password',
    )

    # set new password
    set(
      css: 'input[name="password"]',
      value: 'some',
    )
    set(
      css: 'input[name="password_confirm"]',
      value: 'some2',
    )
    click(css: '.content .btn--primary')
    watch_for(
      css: 'body',
      value: 'passwords do not match',
    )

    set(
      css: 'input[name="password"]',
      value: 'some',
    )
    set(
      css: 'input[name="password_confirm"]',
      value: 'some',
    )
    click(css: '.content .btn--primary')
    watch_for(
      css: 'body',
      value: 'it must be at least',
    )

    set(
      css: 'input[name="password"]',
      value: 'some-pass-new',
    )
    set(
      css: 'input[name="password_confirm"]',
      value: 'some-pass-new',
    )
    click(css: '.content .btn--primary')
    watch_for(
      css: 'body',
      value: 'must contain at least 1 digit',
    )

    set(
      css: 'input[name="password"]',
      value: 'some-pass-new2',
    )
    set(
      css: 'input[name="password_confirm"]',
      value: 'some-pass-new2',
    )
    click(css: '.content .btn--primary')
    watch_for(
      css: 'body',
      value: 'Your password has been changed',
    )

    # check if user is logged in
    sleep 5
    match(
      css: '.user-menu .user a',
      value: signup_user_email,
      attribute: 'title',
    )

  end
end
