summaryrefslogtreecommitdiff
path: root/qa/qa/factory/resource/user.rb
blob: e361face1f03bd12810373a5bf821fa8f2d62f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'securerandom'

module QA
  module Factory
    module Resource
      class User < Factory::Base
        attr_reader :unique_id
        attr_writer :username, :password

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

        def username
          @username ||= "qa-user-#{unique_id}"
        end

        def password
          @password ||= 'password'
        end

        def name
          @name ||= username
        end

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

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

        def fabricate!
          # Don't try to log-out if we're not logged-in
          if Page::Main::Menu.perform { |p| p.has_personal_area?(wait: 0) }
            Page::Main::Menu.perform { |main| main.sign_out }
          end

          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
          end
        end
      end
    end
  end
end