· 8 years ago · Jan 19, 2018, 06:02 PM
1#
2# :class => points to name of model used.
3#
4FactoryGirl.define do
5
6 # User groups
7 factory :role_group, :class => :RoleGroup do
8 name 'client'
9 end
10
11 # Define basic Role object
12 factory :role, :class => :Role do
13 name 'admin'
14 weight '0'
15 group RoleGroup.find_by_name('client')
16 end
17
18 # Define Person object
19 factory :person, :class => :Person do
20 email 'root@test.com'
21 end
22
23 factory :country, :class => :Country do
24 name 'Slovakia'
25 code 'SK'
26 end
27
28 # Create actual user types
29 factory :admin, :class => :User do |u|
30 u.password '123'
31 u.password_confirmation '123'
32 u.aasm_state :active
33 u.role Role.find_by_name("admin")
34 u.person {|person| person.association(:person, :email => "root@test.com")}
35 u.after_build {|user|
36 user.countries << Country.all
37 }
38 end
39
40 factory :central, :class => :User do |u|
41 u.password '123'
42 u.password_confirmation '123'
43 u.aasm_state :active
44 u.role Role.find_by_name("central")
45 u.person {|person| person.association(:person, :email => "central@test.com")}
46 end
47
48 factory :tmm, :class => :User do |u|
49 u.password '123'
50 u.password_confirmation '123'
51 u.aasm_state :active
52 u.role Role.find_by_name("tmm")
53 u.person {|person| person.association(:person, :email => "tmm@test.com")}
54 end
55
56 factory :kam, :class => :User do |u|
57 u.password '123'
58 u.password_confirmation '123'
59 u.aasm_state :active
60 u.role Role.find_by_name("kam")
61 u.person {|person| person.association(:person, :email => "kam@test.com")}
62 end
63
64 factory :ip, :class => :User do |u|
65 u.password '123'
66 u.password_confirmation '123'
67 u.aasm_state :active
68 u.role Role.find_by_name("ip")
69 u.person {|person| person.association(:person, :email => "ip@test.com")}
70 end
71
72end
73
74Given /^I am not authenticated$/ do
75 visit('/logout') # ensure that at least
76end
77
78Given /^the following role groups exists:$/ do |table|
79 puts hash["name"].to_s
80 table.hashes.each do |hash|
81 FactoryGirl.create(:role_group, :name => hash["name"])
82 end
83end
84
85Given /^the following roles exists:$/ do |table|
86 table.hashes.each do |hash|
87 role = FactoryGirl.create(:role, :name => hash["name"], :weight => hash["weight"], :group => RoleGroup.find_by_name(hash["group"]))
88 hash["allowed_roles"].split(",").each {|allowed_role|
89 role.allowed_roles.find_or_create_by_role_ref_id(allowed_role)
90 }
91 end
92end
93
94Given /^the following countries exists:$/ do |table|
95 table.hashes.each do |hash|
96 FactoryGirl.create(:country, :name => hash["name"], :code => hash["code"])
97 end
98end
99
100Given /^the following users exists:$/ do |table|
101 table.hashes.each do |hash|
102 person = Person.find_by_email(hash["email"])
103 if !person
104 FactoryGirl.create(hash["role"].to_sym)
105 end
106 end
107end
108
109Given /^Sign in user with email "([^\"]*)" and password "([^\"]*)" to site$/ do |email, password|
110 Then %{I go to login}
111 And %{I should see "Login"}
112 And %{I fill in "Email" with "#{email}"}
113 And %{I fill in "Password" with "#{password}"}
114 And %{I press "Confirm"}
115end
116
117@login
118Feature: Login to site
119 To test if User Authentication works as expected we try to login with created
120 user name/password and check if we can see expected Logout text in Shop Listings site.
121
122 We try to login different types of users to see if it works.
123
124 Background:
125 Given the following role groups exists:
126 |name|
127 |client|
128 |producer|
129 Given the following roles exists:
130 |name|weight|group|allowed_roles|
131 |ip|3|producer||
132 |kam|3|client||
133 |tmm|2|client|tmm,kam|
134 |central|1|client|central,tmm|
135 |admin|0|client|admin,central,tmm,kam,ip|
136 Given the following countries exists:
137 |name|code|
138 |Slovakia|SK|
139 |Czech Republic|CZ|
140 |Austria|AU|
141 |Hungary|HU|
142 Given the following users exists:
143 |email|password|role|countries|
144 |root@root.com|123|admin|SK,CZ,AU,HU|
145 |central@root.com|123|central|SK,CZ,AU|
146 |tmm@root.com|123|tmm|SK,CZ|
147 |kam@root.com|123|kam|SK|
148 |ip@root.com|123|ip||
149
150 Scenario Outline: Successful Login
151 Given I am not authenticated
152 When I am on the new login page
153 Then I should see "Login"
154 And I fill in "Email" with "<email>"
155 And I fill in "Password" with "<password>"
156 And I press "Confirm"
157 Then I should see "Logout"
158 When I go to "logout"
159 Then I should see "Login"
160
161 Examples:
162 |email|password|
163 |root@root.com|123|
164 |central@root.com|123|
165 |tmm@root.com|123|
166 |kam@root.com|123|
167 |ip@root.com|123|
168
169 Scenario: Failed Login
170 Given I am not authenticated
171 Given the following users exists:
172 |email|password|role|
173 |root@root.com|123|admin|
174 When I am on the new login page
175 Then I should see "Login"
176 And I fill in "Email" with "failing_user@root.com"
177 And I fill in "Password" with "123"
178 And I press "Confirm"
179 Then I should see "Invalid email or password"