solidus_auth_devise_devise_token_auth 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +20 -0
  5. data/CHANGELOG.md +173 -0
  6. data/Gemfile +36 -0
  7. data/LICENSE.md +26 -0
  8. data/README.md +98 -0
  9. data/Rakefile +21 -0
  10. data/app/mailers/spree/user_mailer.rb +15 -0
  11. data/app/models/spree/auth_configuration.rb +9 -0
  12. data/app/models/spree/user.rb +47 -0
  13. data/app/overrides/auth_admin_login_navigation_bar.rb +10 -0
  14. data/app/overrides/auth_shared_login_bar.rb +10 -0
  15. data/bin/rails +7 -0
  16. data/circle.yml +6 -0
  17. data/config/initializers/devise.rb +141 -0
  18. data/config/initializers/warden.rb +14 -0
  19. data/config/locales/de.yml +53 -0
  20. data/config/locales/en.yml +54 -0
  21. data/config/locales/es.yml +50 -0
  22. data/config/locales/fr.yml +50 -0
  23. data/config/locales/it.yml +76 -0
  24. data/config/locales/nl.yml +50 -0
  25. data/config/locales/pt-BR.yml +52 -0
  26. data/config/locales/tr.yml +50 -0
  27. data/config/locales/zh-CN.yml +54 -0
  28. data/config/routes.rb +71 -0
  29. data/db/default/users.rb +75 -0
  30. data/db/migrate/20101026184949_create_users.rb +29 -0
  31. data/db/migrate/20101026184950_rename_columns_for_devise.rb +41 -0
  32. data/db/migrate/20101214150824_convert_user_remember_field.rb +11 -0
  33. data/db/migrate/20120203010234_add_reset_password_sent_at_to_spree_users.rb +8 -0
  34. data/db/migrate/20120605211305_make_users_email_index_unique.rb +9 -0
  35. data/db/migrate/20140904000425_add_deleted_at_to_users.rb +6 -0
  36. data/db/migrate/20141002154641_add_confirmable_to_users.rb +7 -0
  37. data/db/seeds.rb +1 -0
  38. data/lib/assets/javascripts/spree/backend/solidus_auth.js +1 -0
  39. data/lib/assets/javascripts/spree/frontend/solidus_auth.js +1 -0
  40. data/lib/assets/stylesheets/spree/backend/solidus_auth.css +3 -0
  41. data/lib/assets/stylesheets/spree/frontend/solidus_auth.css +3 -0
  42. data/lib/controllers/backend/spree/admin/admin_controller_decorator.rb +11 -0
  43. data/lib/controllers/backend/spree/admin/orders/customer_details_controller_decorator.rb +15 -0
  44. data/lib/controllers/backend/spree/admin/user_passwords_controller.rb +41 -0
  45. data/lib/controllers/backend/spree/admin/user_sessions_controller.rb +43 -0
  46. data/lib/controllers/frontend/spree/checkout_controller_decorator.rb +66 -0
  47. data/lib/controllers/frontend/spree/user_confirmations_controller.rb +14 -0
  48. data/lib/controllers/frontend/spree/user_passwords_controller.rb +50 -0
  49. data/lib/controllers/frontend/spree/user_registrations_controller.rb +40 -0
  50. data/lib/controllers/frontend/spree/user_sessions_controller.rb +64 -0
  51. data/lib/controllers/frontend/spree/users_controller.rb +57 -0
  52. data/lib/generators/solidus/auth/install/install_generator.rb +25 -0
  53. data/lib/generators/solidus/auth/install/templates/config/initializers/devise.rb +1 -0
  54. data/lib/solidus/auth.rb +2 -0
  55. data/lib/solidus_auth_devise.rb +13 -0
  56. data/lib/spree/auth/devise.rb +14 -0
  57. data/lib/spree/auth/engine.rb +80 -0
  58. data/lib/spree/authentication_helpers.rb +33 -0
  59. data/lib/tasks/auth.rake +9 -0
  60. data/lib/views/backend/spree/admin/shared/_navigation_footer.html.erb +25 -0
  61. data/lib/views/backend/spree/admin/user_passwords/edit.html.erb +15 -0
  62. data/lib/views/backend/spree/admin/user_passwords/new.html.erb +17 -0
  63. data/lib/views/backend/spree/admin/user_sessions/authorization_failure.html.erb +4 -0
  64. data/lib/views/backend/spree/admin/user_sessions/new.html.erb +31 -0
  65. data/lib/views/backend/spree/layouts/admin/_login_nav.html.erb +13 -0
  66. data/lib/views/frontend/spree/checkout/registration.html.erb +22 -0
  67. data/lib/views/frontend/spree/shared/_flashes.html.erb +9 -0
  68. data/lib/views/frontend/spree/shared/_login.html.erb +18 -0
  69. data/lib/views/frontend/spree/shared/_login_bar.html.erb +2 -0
  70. data/lib/views/frontend/spree/shared/_login_bar_items.html.erb +6 -0
  71. data/lib/views/frontend/spree/shared/_user_form.html.erb +17 -0
  72. data/lib/views/frontend/spree/user_mailer/confirmation_instructions.text.erb +5 -0
  73. data/lib/views/frontend/spree/user_mailer/reset_password_instructions.text.erb +10 -0
  74. data/lib/views/frontend/spree/user_passwords/edit.html.erb +17 -0
  75. data/lib/views/frontend/spree/user_passwords/new.html.erb +17 -0
  76. data/lib/views/frontend/spree/user_registrations/new.html.erb +21 -0
  77. data/lib/views/frontend/spree/user_sessions/authorization_failure.html.erb +4 -0
  78. data/lib/views/frontend/spree/user_sessions/new.html.erb +13 -0
  79. data/lib/views/frontend/spree/users/edit.html.erb +14 -0
  80. data/lib/views/frontend/spree/users/show.html.erb +43 -0
  81. data/solidus_auth_devise.gemspec +41 -0
  82. data/spec/controllers/spree/checkout_controller_spec.rb +196 -0
  83. data/spec/controllers/spree/products_controller_spec.rb +27 -0
  84. data/spec/controllers/spree/user_passwords_controller_spec.rb +44 -0
  85. data/spec/controllers/spree/user_registrations_controller_spec.rb +96 -0
  86. data/spec/controllers/spree/user_sessions_controller_spec.rb +113 -0
  87. data/spec/controllers/spree/users_controller_spec.rb +38 -0
  88. data/spec/factories/confirmed_user.rb +7 -0
  89. data/spec/features/account_spec.rb +58 -0
  90. data/spec/features/admin/orders_spec.rb +30 -0
  91. data/spec/features/admin/password_reset_spec.rb +24 -0
  92. data/spec/features/admin/products_spec.rb +9 -0
  93. data/spec/features/admin/sign_in_spec.rb +44 -0
  94. data/spec/features/admin/sign_out_spec.rb +22 -0
  95. data/spec/features/admin_permissions_spec.rb +46 -0
  96. data/spec/features/change_email_spec.rb +26 -0
  97. data/spec/features/checkout_spec.rb +181 -0
  98. data/spec/features/confirmation_spec.rb +32 -0
  99. data/spec/features/order_spec.rb +62 -0
  100. data/spec/features/password_reset_spec.rb +24 -0
  101. data/spec/features/sign_in_spec.rb +52 -0
  102. data/spec/features/sign_out_spec.rb +25 -0
  103. data/spec/features/sign_up_spec.rb +30 -0
  104. data/spec/mailers/user_mailer_spec.rb +46 -0
  105. data/spec/models/order_spec.rb +26 -0
  106. data/spec/models/user_spec.rb +63 -0
  107. data/spec/spec_helper.rb +17 -0
  108. data/spec/support/ability.rb +15 -0
  109. data/spec/support/authentication_helpers.rb +14 -0
  110. data/spec/support/confirm_helpers.rb +13 -0
  111. data/spec/support/email.rb +5 -0
  112. data/spec/support/preferences.rb +5 -0
  113. data/spec/support/spree.rb +10 -0
  114. metadata +446 -0
@@ -0,0 +1,21 @@
1
+ <% @body_id = 'signup' %>
2
+
3
+ <%= render 'spree/shared/error_messages', target: resource %>
4
+
5
+ <div id="new-customer">
6
+ <h6><%= Spree.t(:new_customer) %></h6>
7
+
8
+ <div data-hook="signup">
9
+ <%= form_for resource, as: :spree_user, url: spree.registration_path(resource) do |f| %>
10
+ <div data-hook="signup_inside_form">
11
+ <%= render partial: 'spree/shared/user_form', locals: { f: f } %>
12
+ <p><%= f.submit Spree.t(:create), class: 'button primary' %></p>
13
+ </div>
14
+ <% end %>
15
+ <%= Spree.t(:or) %>&nbsp;<%= link_to Spree.t(:login_as_existing), spree.login_path %>
16
+
17
+ </div>
18
+
19
+ </div>
20
+
21
+ <div data-hook="login_extras"></div>
@@ -0,0 +1,4 @@
1
+ <div style="height:50px; padding-top:20px;">
2
+ <strong><%= Spree.t(:authorization_failure) %></strong>
3
+ </div>
4
+ <!-- Add your own custom access denied message here if you like -->
@@ -0,0 +1,13 @@
1
+ <% if flash[:alert] %>
2
+ <div class="flash errors"><%= flash[:alert] %></div>
3
+ <% end %>
4
+
5
+ <% @body_id = 'login' %>
6
+ <div id="existing-customer">
7
+ <h6><%= Spree.t(:login_as_existing) %></h6>
8
+ <div data-hook="login">
9
+ <%= render partial: 'spree/shared/login' %>
10
+ <%= Spree.t(:or) %>&nbsp;<%= link_to Spree.t(:create_a_new_account), spree.signup_path %> | <%= link_to Spree.t(:forgot_password), spree.recover_password_path %>
11
+ </div>
12
+ </div>
13
+ <div data-hook="login_extras"></div>
@@ -0,0 +1,14 @@
1
+ <%= render partial: 'spree/shared/error_messages', locals: { target: @user } %>
2
+
3
+ <div id="edit-account">
4
+ <h1><%= Spree.t(:editing_user) %></h1>
5
+
6
+ <div data-hook="account_edit">
7
+ <%= form_for Spree::User.new, as: @user, url: spree.user_path(@user), method: :put do |f| %>
8
+ <%= render partial: 'spree/shared/user_form', locals: { f: f } %>
9
+ <p>
10
+ <%= f.submit Spree.t(:update), class: 'button primary' %>
11
+ </p>
12
+ <% end %>
13
+ </div>
14
+ </div>
@@ -0,0 +1,43 @@
1
+ <h1><%= accurate_title %></h1>
2
+
3
+ <div data-hook="account_summary" class="account-summary">
4
+ <dl id="user-info">
5
+ <dt><%= Spree.t(:email) %></dt>
6
+ <dd><%= @user.email %> (<%= link_to Spree.t(:edit), spree.edit_account_path %>)</dd>
7
+ </dl>
8
+ </div>
9
+
10
+ <div data-hook="account_my_orders" class="account-my-orders">
11
+
12
+ <h3><%= Spree.t(:my_orders) %></h3>
13
+ <% if @orders.present? %>
14
+ <table class="order-summary">
15
+ <thead>
16
+ <tr>
17
+ <th class="order-number"><%= I18n.t(:number, scope: 'activerecord.attributes.spree/order') %></th>
18
+ <th class="order-date"><%= Spree.t(:date) %></th>
19
+ <th class="order-status"><%= Spree.t(:status) %></th>
20
+ <th class="order-payment-state"><%= Spree.t(:payment_state) %></th>
21
+ <th class="order-shipment-state"><%= Spree.t(:shipment_state) %></th>
22
+ <th class="order-total"><%= Spree.t(:total) %></th>
23
+ </tr>
24
+ </thead>
25
+ <tbody>
26
+ <% @orders.each do |order| %>
27
+ <tr class="<%= cycle('even', 'odd') %>">
28
+ <td class="order-number"><%= link_to order.number, order_url(order) %></td>
29
+ <td class="order-date"><%= l order.completed_at.to_date %></td>
30
+ <td class="order-status"><%= Spree.t("order_state.#{order.state}").titleize %></td>
31
+ <td class="order-payment-state"><%= Spree.t("payment_states.#{order.payment_state}").titleize if order.payment_state %></td>
32
+ <td class="order-shipment-state"><%= Spree.t("shipment_states.#{order.shipment_state}").titleize if order.shipment_state %></td>
33
+ <td class="order-total"><%= order.display_total %></td>
34
+ </tr>
35
+ <% end %>
36
+ </tbody>
37
+ </table>
38
+ <% else %>
39
+ <p><%= Spree.t(:you_have_no_orders_yet) %></p>
40
+ <% end %>
41
+ <br />
42
+
43
+ </div>
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.platform = Gem::Platform::RUBY
5
+ s.name = 'solidus_auth_devise_devise_token_auth'
6
+ s.version = '2.1.0'
7
+ s.summary = 'Provides authentication and authorization services for use with Solidus by using Devise and CanCan (devise_token_auth revised version)'
8
+ s.description = s.summary
9
+
10
+ s.author = 'Michał Siwek (skycocker)'
11
+ s.email = 'mike21@aol.pl'
12
+
13
+ s.required_ruby_version = '>= 2.1'
14
+ s.license = %q{BSD-3}
15
+
16
+ s.files = `git ls-files`.split($\)
17
+ s.test_files = `git ls-files -- spec/*`.split($\)
18
+ s.require_path = 'lib'
19
+ s.requirements << 'none'
20
+
21
+ solidus_version = ['>= 1.2.0', '< 3']
22
+
23
+ s.add_dependency 'solidus_core_devise_token_auth', solidus_version
24
+ s.add_dependency 'solidus_support_devise_token_auth', '>= 0.1.3'
25
+ s.add_dependency 'devise', '~> 4.1'
26
+ s.add_dependency 'devise-encryptable', '0.2.0'
27
+
28
+ s.add_development_dependency 'capybara', '~> 2.14'
29
+ s.add_development_dependency 'capybara-screenshot'
30
+ s.add_development_dependency 'coffee-rails'
31
+ s.add_development_dependency 'database_cleaner', '~> 1.6'
32
+ s.add_development_dependency 'ffaker'
33
+ s.add_development_dependency 'poltergeist', '~> 1.5'
34
+ s.add_development_dependency 'rspec-rails', '~> 3.3'
35
+ s.add_development_dependency 'sass-rails'
36
+ s.add_development_dependency 'shoulda-matchers', '~> 3.1'
37
+ s.add_development_dependency 'simplecov', '~> 0.14'
38
+ s.add_development_dependency 'solidus_backend_devise_token_auth', solidus_version
39
+ s.add_development_dependency 'solidus_frontend_devise_token_auth', solidus_version
40
+ s.add_development_dependency 'sqlite3'
41
+ end
@@ -0,0 +1,196 @@
1
+ RSpec.describe Spree::CheckoutController, type: :controller do
2
+
3
+ let(:order) { create(:order_with_line_items, email: nil, user: nil, guest_token: token) }
4
+ let(:user) { build(:user) }
5
+ let(:token) { 'some_token' }
6
+ let(:cookie_token) { token }
7
+
8
+ before do
9
+ request.cookie_jar.signed[:guest_token] = cookie_token
10
+ allow(controller).to receive(:current_order) { order }
11
+ allow(order).to receive(:confirmation_required?) { true }
12
+ end
13
+
14
+ context '#edit' do
15
+ context 'when registration step enabled' do
16
+ context 'when authenticated as registered user' do
17
+ before { allow(controller).to receive(:spree_current_user) { user } }
18
+
19
+ it 'proceeds to the first checkout step' do
20
+ get :edit, params: { state: 'address' }
21
+ expect(response).to render_template :edit
22
+ end
23
+ end
24
+
25
+ context 'when not authenticated as guest' do
26
+ it 'redirects to registration step' do
27
+ get :edit, params: { state: 'address' }
28
+ expect(response).to redirect_to spree.checkout_registration_path
29
+ end
30
+ end
31
+
32
+ context 'when authenticated as guest' do
33
+ before { order.email = 'guest@solidus.io' }
34
+
35
+ it 'proceeds to the first checkout step' do
36
+ get :edit, params: { state: 'address' }
37
+ expect(response).to render_template :edit
38
+ end
39
+
40
+ context 'when guest checkout not allowed' do
41
+ before do
42
+ Spree::Config.set(allow_guest_checkout: false)
43
+ end
44
+
45
+ after do
46
+ Spree::Config.set(allow_guest_checkout: true)
47
+ end
48
+
49
+ it 'redirects to registration step' do
50
+ get :edit, params: { state: 'address' }
51
+ expect(response).to redirect_to spree.checkout_registration_path
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ context 'when registration step disabled' do
58
+ before do
59
+ Spree::Auth::Config.set(registration_step: false)
60
+ end
61
+
62
+ context 'when authenticated as registered' do
63
+ before { allow(controller).to receive(:spree_current_user) { user } }
64
+
65
+ it 'proceeds to the first checkout step' do
66
+ get :edit, params: { state: 'address' }
67
+ expect(response).to render_template :edit
68
+ end
69
+ end
70
+
71
+ context 'when authenticated as guest' do
72
+ it 'proceeds to the first checkout step' do
73
+ get :edit, params: { state: 'address' }
74
+ expect(response).to render_template :edit
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ context '#update' do
81
+ context 'when in the confirm state' do
82
+ before do
83
+ order.update_column(:email, 'spree@example.com')
84
+ order.update_column(:state, 'confirm')
85
+
86
+ # So that the order can transition to complete successfully
87
+ allow(order).to receive(:payment_required?) { false }
88
+ end
89
+
90
+ context 'with a token' do
91
+ before { allow(order).to receive(:guest_token) { 'ABC' } }
92
+
93
+ it 'redirects to the tokenized order view' do
94
+ request.cookie_jar.signed[:guest_token] = 'ABC'
95
+ post :update, params: { state: 'confirm' }
96
+ expect(response).to redirect_to spree.token_order_path(order, 'ABC')
97
+ expect(flash.notice).to eq Spree.t(:order_processed_successfully)
98
+ end
99
+ end
100
+
101
+ context 'with a registered user' do
102
+ before do
103
+ allow(controller).to receive(:spree_current_user) { user }
104
+ allow(order).to receive(:user) { user }
105
+ allow(order).to receive(:guest_token) { nil }
106
+ end
107
+
108
+ it 'redirects to the standard order view' do
109
+ post :update, params: { state: 'confirm' }
110
+ expect(response).to redirect_to spree.order_path(order)
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ context '#registration' do
117
+ it 'does not check registration' do
118
+ expect(controller).not_to receive(:check_registration)
119
+ get :registration
120
+ end
121
+
122
+ it 'checks if the user is authorized for :edit' do
123
+ expect(controller).to receive(:authorize!).with(:edit, order, token)
124
+ request.cookie_jar.signed[:guest_token] = token
125
+ get :registration, params: {}
126
+ end
127
+ end
128
+
129
+ context '#update_registration' do
130
+ subject { put :update_registration, params: { order: { email: email } } }
131
+ let(:email) { 'foo@example.com' }
132
+
133
+ it 'does not check registration' do
134
+ expect(controller).not_to receive(:check_registration)
135
+ subject
136
+ end
137
+
138
+ it 'redirects to the checkout_path after saving' do
139
+ subject
140
+ expect(response).to redirect_to spree.checkout_path
141
+ end
142
+
143
+ # Regression test for https://github.com/solidusio/solidus/issues/1588
144
+ context 'order in address state' do
145
+ let(:order) do
146
+ create(
147
+ :order_with_line_items,
148
+ email: nil,
149
+ user: nil,
150
+ guest_token: token,
151
+ bill_address: nil,
152
+ ship_address: nil,
153
+ state: 'address'
154
+ )
155
+ end
156
+
157
+ # This may seem out of left field, but previously there was an issue
158
+ # where address would be built in a before filter and then would be saved
159
+ # when trying to update the email.
160
+ it "doesn't create addresses" do
161
+ expect {
162
+ subject
163
+ }.not_to change { Spree::Address.count }
164
+ expect(response).to redirect_to spree.checkout_path
165
+ end
166
+ end
167
+
168
+ context 'invalid email' do
169
+ let(:email) { 'invalid' }
170
+
171
+ it 'renders the registration view' do
172
+ subject
173
+ expect(flash[:registration_error]).to eq I18n.t(:email_is_invalid, scope: [:errors, :messages])
174
+ expect(response).to render_template :registration
175
+ end
176
+ end
177
+
178
+ context 'with wrong order token' do
179
+ let(:cookie_token) { 'lol_no_access' }
180
+
181
+ it 'redirects to login' do
182
+ put :update_registration, params: { order: { email: 'foo@example.com' } }
183
+ expect(response).to redirect_to(login_path)
184
+ end
185
+ end
186
+
187
+ context 'without order token' do
188
+ let(:cookie_token) { nil }
189
+
190
+ it 'redirects to login' do
191
+ put :update_registration, params: { order: { email: 'foo@example.com' } }
192
+ expect(response).to redirect_to(login_path)
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,27 @@
1
+ RSpec.describe Spree::ProductsController, type: :controller do
2
+
3
+ let!(:product) { create(:product, available_on: 1.year.from_now) }
4
+ let!(:user) { build(:user) }
5
+
6
+ it 'allows admins to view non-active products' do
7
+ allow(controller).to receive(:before_save_new_order)
8
+ allow(controller).to receive(:spree_current_user) { user }
9
+ allow(user).to receive(:has_spree_role?) { true }
10
+ get :show, params: { id: product.to_param }
11
+ expect(response.status).to eq(200)
12
+ end
13
+
14
+ it 'cannot view non-active products' do
15
+ allow(controller).to receive(:before_save_new_order)
16
+ allow(controller).to receive(:spree_current_user) { user }
17
+ allow(user).to receive(:has_spree_role?) { false }
18
+ if SolidusSupport.solidus_gem_version < Gem::Version.new('2.5.x')
19
+ get :show, params: { id: product.to_param }
20
+ expect(response.status).to eq(404)
21
+ else
22
+ expect {
23
+ get :show, params: { id: product.to_param }
24
+ }.to raise_error(ActiveRecord::RecordNotFound)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,44 @@
1
+ RSpec.describe Spree::UserPasswordsController, type: :controller do
2
+
3
+ let(:token) { 'some_token' }
4
+
5
+ before { @request.env['devise.mapping'] = Devise.mappings[:spree_user] }
6
+
7
+ describe 'GET edit' do
8
+ context 'when the user token has not been specified' do
9
+ it 'redirects to the new session path' do
10
+ get :edit
11
+ expect(response).to redirect_to(
12
+ 'http://test.host/user/spree_user/sign_in'
13
+ )
14
+ end
15
+
16
+ it 'flashes an error' do
17
+ get :edit
18
+ expect(flash[:alert]).to include(
19
+ "You can't access this page without coming from a password reset " +
20
+ 'email'
21
+ )
22
+ end
23
+ end
24
+
25
+ context 'when the user token has been specified' do
26
+ it 'does something' do
27
+ get :edit, params: { reset_password_token: token }
28
+ expect(response.code).to eq('200')
29
+ end
30
+ end
31
+ end
32
+
33
+ context '#update' do
34
+ context 'when updating password with blank password' do
35
+ it 'shows error flash message, sets spree_user with token and re-displays password edit form' do
36
+ put :update, params: { spree_user: { password: '', password_confirmation: '', reset_password_token: token } }
37
+ expect(assigns(:spree_user).kind_of?(Spree::User)).to eq true
38
+ expect(assigns(:spree_user).reset_password_token).to eq token
39
+ expect(flash[:error]).to eq I18n.t(:cannot_be_blank, scope: [:devise, :user_passwords, :spree_user])
40
+ expect(response).to render_template :edit
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,96 @@
1
+ RSpec.describe Spree::UserRegistrationsController, type: :controller do
2
+
3
+ before { @request.env['devise.mapping'] = Devise.mappings[:spree_user] }
4
+
5
+ context '#create' do
6
+ before do
7
+ allow(controller).to receive(:after_sign_up_path_for) do
8
+ spree.root_path(thing: 7)
9
+ end
10
+ end
11
+
12
+ let(:password_confirmation) { 'foobar123' }
13
+
14
+ subject do
15
+ post(:create, {
16
+ params: {
17
+ spree_user: {
18
+ email: 'foobar@example.com',
19
+ password: 'foobar123',
20
+ password_confirmation: password_confirmation
21
+ }
22
+ }
23
+ })
24
+ end
25
+
26
+ context 'when user created successfuly' do
27
+ it 'saves the user' do
28
+ expect { subject }.to change { Spree::User.count }.from(0).to(1)
29
+ end
30
+
31
+ it 'sets flash message' do
32
+ subject
33
+ expect(flash[:notice]).to eq('Welcome! You have signed up successfully.')
34
+ end
35
+
36
+ it 'signs in user' do
37
+ expect(controller.warden).to receive(:set_user)
38
+ subject
39
+ end
40
+
41
+ it 'sets spree_user_signup session' do
42
+ subject
43
+ expect(session[:spree_user_signup]).to be true
44
+ end
45
+
46
+ it 'redirects to after_sign_up path' do
47
+ subject
48
+ expect(response).to redirect_to spree.root_path(thing: 7)
49
+ end
50
+
51
+ context 'with a guest token present' do
52
+ before do
53
+ request.cookie_jar.signed[:guest_token] = 'ABC'
54
+ end
55
+
56
+ it 'assigns orders with the correct token and no user present' do
57
+ order = create(:order, guest_token: 'ABC', user_id: nil, created_by_id: nil)
58
+ subject
59
+ user = Spree::User.find_by_email('foobar@example.com')
60
+
61
+ order.reload
62
+ expect(order.user_id).to eq user.id
63
+ expect(order.created_by_id).to eq user.id
64
+ end
65
+
66
+ it 'does not assign orders with an existing user' do
67
+ order = create(:order, guest_token: 'ABC', user_id: 200)
68
+ subject
69
+
70
+ expect(order.reload.user_id).to eq 200
71
+ end
72
+
73
+ it 'does not assign orders with a different token' do
74
+ order = create(:order, guest_token: 'DEF', user_id: nil, created_by_id: nil)
75
+ subject
76
+
77
+ expect(order.reload.user_id).to be_nil
78
+ end
79
+ end
80
+ end
81
+
82
+ context 'when user not valid' do
83
+ let(:password_confirmation) { 'foobard123' }
84
+
85
+ it 'resets password fields' do
86
+ expect(controller).to receive(:clean_up_passwords)
87
+ subject
88
+ end
89
+
90
+ it 'renders new view' do
91
+ subject
92
+ expect(:response).to render_template(:new)
93
+ end
94
+ end
95
+ end
96
+ end