user.rb 1.27 KB
Newer Older
1 2 3 4 5 6
require 'securerandom'

module QA
  module Factory
    module Resource
      class User < Factory::Base
7
        attr_reader :unique_id
8
        attr_writer :username, :password
9 10

        def initialize
11
          @unique_id = SecureRandom.hex(8)
12 13
        end

14 15 16
        def username
          @username ||= "qa-user-#{unique_id}"
        end
17

18 19 20
        def password
          @password ||= 'password'
        end
21

22 23 24 25 26 27 28 29 30 31 32
        def name
          @name ||= username
        end

        def email
          @email ||= "#{username}@example.com"
        end

        def credentials_given?
          defined?(@username) && defined?(@password)
        end
33 34

        def fabricate!
35
          # Don't try to log-out if we're not logged-in
36
          if Page::Main::Menu.perform { |p| p.has_personal_area?(wait: 0) }
sliaquat's avatar
sliaquat committed
37
            Page::Main::Menu.perform { |main| main.sign_out }
38
          end
39 40 41 42 43 44 45 46 47 48 49 50

          if credentials_given?
            Page::Main::Login.perform do |login|
              login.sign_in_using_credentials(self)
            end
          else
            Page::Main::Login.perform do |login|
              login.switch_to_register_tab
            end
            Page::Main::SignUp.perform do |signup|
              signup.sign_up!(self)
            end
51 52 53 54 55 56
          end
        end
      end
    end
  end
end