curupira 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of curupira might be problematic. Click here for more details.

Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +19 -0
  5. data/Gemfile +17 -0
  6. data/Gemfile.lock +202 -0
  7. data/README.md +72 -0
  8. data/Rakefile +31 -0
  9. data/app/controllers/curupira/authorized_controller.rb +9 -0
  10. data/app/controllers/curupira/groups_controller.rb +43 -0
  11. data/app/controllers/curupira/passwords_controller.rb +46 -0
  12. data/app/controllers/curupira/roles_controller.rb +43 -0
  13. data/app/controllers/curupira/sessions_controller.rb +30 -0
  14. data/app/controllers/curupira/users_controller.rb +47 -0
  15. data/app/helpers/curupira/authorize_helper.rb +21 -0
  16. data/app/helpers/curupira/relationship_select_options_helper.rb +17 -0
  17. data/app/mailers/curupira/reset_password_mailer.rb +13 -0
  18. data/app/views/curupira/groups/_form.html.erb +28 -0
  19. data/app/views/curupira/groups/_role_group_fields.html.erb +9 -0
  20. data/app/views/curupira/groups/edit.html.erb +3 -0
  21. data/app/views/curupira/groups/index.html.erb +24 -0
  22. data/app/views/curupira/groups/new.html.erb +3 -0
  23. data/app/views/curupira/groups/show.html.erb +32 -0
  24. data/app/views/curupira/passwords/edit.html.erb +14 -0
  25. data/app/views/curupira/passwords/new.html.erb +14 -0
  26. data/app/views/curupira/reset_password_mailer/instructions.html.erb +19 -0
  27. data/app/views/curupira/reset_password_mailer/reseted.html.erb +7 -0
  28. data/app/views/curupira/roles/_authorization_fields.html.erb +10 -0
  29. data/app/views/curupira/roles/_form.html.erb +23 -0
  30. data/app/views/curupira/roles/edit.html.erb +3 -0
  31. data/app/views/curupira/roles/index.html.erb +24 -0
  32. data/app/views/curupira/roles/new.html.erb +3 -0
  33. data/app/views/curupira/roles/show.html.erb +37 -0
  34. data/app/views/curupira/sessions/new.html.erb +17 -0
  35. data/app/views/curupira/shared/_model_links.html.erb +7 -0
  36. data/app/views/curupira/shared/_session_links.html.erb +7 -0
  37. data/app/views/curupira/users/_form.html.erb +43 -0
  38. data/app/views/curupira/users/_group_user_fields.html.erb +20 -0
  39. data/app/views/curupira/users/_role_group_user_fields.html.erb +10 -0
  40. data/app/views/curupira/users/edit.html.erb +3 -0
  41. data/app/views/curupira/users/index.html.erb +28 -0
  42. data/app/views/curupira/users/new.html.erb +3 -0
  43. data/app/views/curupira/users/show.html.erb +28 -0
  44. data/curupira.gemspec +28 -0
  45. data/lib/curupira.rb +16 -0
  46. data/lib/curupira/authorizer.rb +43 -0
  47. data/lib/curupira/rails.rb +30 -0
  48. data/lib/curupira/rails/routes.rb +15 -0
  49. data/lib/curupira/version.rb +3 -0
  50. data/lib/generators/curupira/controllers/controllers_generator.rb +27 -0
  51. data/lib/generators/curupira/install/install_generator.rb +80 -0
  52. data/lib/generators/curupira/install/model_generators_helper.rb +266 -0
  53. data/lib/generators/curupira/routes/routes_generator.rb +21 -0
  54. data/lib/generators/curupira/routes/templates/routes.rb +1 -0
  55. data/lib/generators/curupira/templates/db/migrate/add_curupira_to_users.rb +20 -0
  56. data/lib/generators/curupira/templates/db/migrate/create_action_labels.rb +12 -0
  57. data/lib/generators/curupira/templates/db/migrate/create_authorizations.rb +12 -0
  58. data/lib/generators/curupira/templates/db/migrate/create_features.rb +11 -0
  59. data/lib/generators/curupira/templates/db/migrate/create_group_users.rb +13 -0
  60. data/lib/generators/curupira/templates/db/migrate/create_groups.rb +10 -0
  61. data/lib/generators/curupira/templates/db/migrate/create_role_group_users.rb +12 -0
  62. data/lib/generators/curupira/templates/db/migrate/create_role_groups.rb +13 -0
  63. data/lib/generators/curupira/templates/db/migrate/create_roles.rb +8 -0
  64. data/lib/generators/curupira/templates/db/migrate/sorcery_core.rb +27 -0
  65. data/lib/generators/curupira/templates/models/action_label.rb +2 -0
  66. data/lib/generators/curupira/templates/models/authorization.rb +2 -0
  67. data/lib/generators/curupira/templates/models/feature.rb +2 -0
  68. data/lib/generators/curupira/templates/models/feature_service.rb +4 -0
  69. data/lib/generators/curupira/templates/models/group.rb +2 -0
  70. data/lib/generators/curupira/templates/models/group_user.rb +2 -0
  71. data/lib/generators/curupira/templates/models/role.rb +2 -0
  72. data/lib/generators/curupira/templates/models/role_group.rb +2 -0
  73. data/lib/generators/curupira/templates/models/role_group_user.rb +2 -0
  74. data/lib/generators/curupira/templates/models/service.rb +2 -0
  75. data/lib/generators/curupira/templates/models/user.rb +2 -0
  76. data/lib/generators/curupira/templates/sorcery.rb +462 -0
  77. data/lib/generators/curupira/views/views_generator.rb +27 -0
  78. data/lib/tasks/curupira_tasks.rake +4 -0
  79. data/spec/app_templates/app/models/action_label.rb +2 -0
  80. data/spec/app_templates/app/models/authorization.rb +2 -0
  81. data/spec/app_templates/app/models/feature.rb +2 -0
  82. data/spec/app_templates/app/models/feature_service.rb +2 -0
  83. data/spec/app_templates/app/models/group.rb +4 -0
  84. data/spec/app_templates/app/models/group_user.rb +2 -0
  85. data/spec/app_templates/app/models/role.rb +2 -0
  86. data/spec/app_templates/app/models/role_group.rb +2 -0
  87. data/spec/app_templates/app/models/role_group_user.rb +2 -0
  88. data/spec/app_templates/app/models/service.rb +2 -0
  89. data/spec/app_templates/app/models/user.rb +2 -0
  90. data/spec/app_templates/config/routes.rb +3 -0
  91. data/spec/controllers/controller_spec.rb +40 -0
  92. data/spec/controllers/groups_controller_spec.rb +162 -0
  93. data/spec/controllers/passwords_controller_spec.rb +171 -0
  94. data/spec/controllers/roles_controller_spec.rb +168 -0
  95. data/spec/controllers/sessions_controller_spec.rb +112 -0
  96. data/spec/controllers/users_controller_spec.rb +182 -0
  97. data/spec/curupira/authorization_spec.rb +67 -0
  98. data/spec/dummy/README.rdoc +28 -0
  99. data/spec/dummy/Rakefile +6 -0
  100. data/spec/dummy/app/assets/images/.keep +0 -0
  101. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  102. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  103. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  104. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  105. data/spec/dummy/app/controllers/home_controller.rb +4 -0
  106. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  107. data/spec/dummy/app/mailers/.keep +0 -0
  108. data/spec/dummy/app/models/.keep +0 -0
  109. data/spec/dummy/app/models/action_label.rb +3 -0
  110. data/spec/dummy/app/models/authorization.rb +4 -0
  111. data/spec/dummy/app/models/feature.rb +3 -0
  112. data/spec/dummy/app/models/group.rb +9 -0
  113. data/spec/dummy/app/models/group_user.rb +7 -0
  114. data/spec/dummy/app/models/role.rb +10 -0
  115. data/spec/dummy/app/models/role_group.rb +4 -0
  116. data/spec/dummy/app/models/role_group_user.rb +4 -0
  117. data/spec/dummy/app/models/user.rb +10 -0
  118. data/spec/dummy/app/views/home/index.html.erb +5 -0
  119. data/spec/dummy/app/views/home/test.html.erb +1 -0
  120. data/spec/dummy/app/views/layouts/application.html.erb +19 -0
  121. data/spec/dummy/bin/bundle +3 -0
  122. data/spec/dummy/bin/rails +4 -0
  123. data/spec/dummy/bin/rake +4 -0
  124. data/spec/dummy/bin/setup +29 -0
  125. data/spec/dummy/config.ru +4 -0
  126. data/spec/dummy/config/application.rb +25 -0
  127. data/spec/dummy/config/boot.rb +5 -0
  128. data/spec/dummy/config/database.yml +25 -0
  129. data/spec/dummy/config/environment.rb +5 -0
  130. data/spec/dummy/config/environments/development.rb +46 -0
  131. data/spec/dummy/config/environments/production.rb +79 -0
  132. data/spec/dummy/config/environments/test.rb +44 -0
  133. data/spec/dummy/config/initializers/assets.rb +11 -0
  134. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  135. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  136. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  137. data/spec/dummy/config/initializers/inflections.rb +16 -0
  138. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  139. data/spec/dummy/config/initializers/session_store.rb +3 -0
  140. data/spec/dummy/config/initializers/sorcery.rb +462 -0
  141. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  142. data/spec/dummy/config/locales/en.yml +23 -0
  143. data/spec/dummy/config/routes.rb +6 -0
  144. data/spec/dummy/config/secrets.yml +22 -0
  145. data/spec/dummy/db/migrate/20150305011941_create_role_group_users.rb +12 -0
  146. data/spec/dummy/db/migrate/20150305011942_create_authorizations.rb +12 -0
  147. data/spec/dummy/db/migrate/20150305011944_create_groups.rb +10 -0
  148. data/spec/dummy/db/migrate/20150305011945_create_roles.rb +8 -0
  149. data/spec/dummy/db/migrate/20150305011946_sorcery_core.rb +27 -0
  150. data/spec/dummy/db/migrate/20150305011947_create_group_users.rb +13 -0
  151. data/spec/dummy/db/migrate/20150305011948_create_role_groups.rb +13 -0
  152. data/spec/dummy/db/migrate/20150312181229_create_action_labels.rb +12 -0
  153. data/spec/dummy/db/migrate/20150312181747_create_features.rb +11 -0
  154. data/spec/dummy/db/schema.rb +112 -0
  155. data/spec/dummy/db/seeds.rb +137 -0
  156. data/spec/dummy/lib/assets/.keep +0 -0
  157. data/spec/dummy/log/.keep +0 -0
  158. data/spec/dummy/public/404.html +67 -0
  159. data/spec/dummy/public/422.html +67 -0
  160. data/spec/dummy/public/500.html +66 -0
  161. data/spec/dummy/public/favicon.ico +0 -0
  162. data/spec/features/link_authorize_spec.rb +25 -0
  163. data/spec/features/list_users_spec.rb +38 -0
  164. data/spec/features/log_in_spec.rb +15 -0
  165. data/spec/generators/curupira/install/action_label_spec.rb +53 -0
  166. data/spec/generators/curupira/install/authorization_generator_spec.rb +51 -0
  167. data/spec/generators/curupira/install/feature_generator_spec.rb +52 -0
  168. data/spec/generators/curupira/install/group_user_generator_spec.rb +51 -0
  169. data/spec/generators/curupira/install/install_generators_spec.rb +159 -0
  170. data/spec/generators/curupira/install/role_generator_spec.rb +38 -0
  171. data/spec/generators/curupira/install/role_group_generator_spec.rb +51 -0
  172. data/spec/generators/curupira/install/role_group_user_generator_spec.rb +52 -0
  173. data/spec/helpers/relationship_select_options_helper_spec.rb +18 -0
  174. data/spec/rails_helper.rb +55 -0
  175. data/spec/spec_helper.rb +87 -0
  176. data/spec/support/factories.rb +64 -0
  177. data/spec/support/generator_spec_helpers.rb +42 -0
  178. data/spec/support/helpers_spec.rb +23 -0
  179. data/spec/support/shared_examples/role_generator_shared_example_spec.rb +13 -0
  180. data/spec/support/sorcery.rb +4 -0
  181. metadata +539 -0
@@ -0,0 +1,27 @@
1
+ module Curupira
2
+ module Generators
3
+ class ViewsGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../" * 5, __FILE__)
5
+
6
+ def create_views
7
+ views.each do |view|
8
+ copy_file view
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def views
15
+ files_within_root('.', 'app/views/**/*.*')
16
+ end
17
+
18
+ def files_within_root(prefix, glob)
19
+ root = "#{self.class.source_root}/#{prefix}"
20
+
21
+ Dir["#{root}/#{glob}"].sort.map do |full_path|
22
+ full_path.sub(root, '.').gsub('/./', '/')
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :curupira do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,2 @@
1
+ class ActionLabel < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Authorization < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Feature < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class FeatureService < ActiveRecord::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class Group < ActiveRecord::Base
2
+ has_many :group_users
3
+ has_many :users, through: :group_users
4
+ end
@@ -0,0 +1,2 @@
1
+ class GroupUser < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Role < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class RoleGroup < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class RoleGroupUser < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Service < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ root to: "home#index"
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'rails_helper'
2
+
3
+ class BaseController < ApplicationController
4
+ before_filter :authorize
5
+ end
6
+
7
+ describe BaseController, type: :controller do
8
+ let!(:current_user) { FactoryGirl.create(:user) }
9
+
10
+ before do
11
+ setup_authorization(current_user, "base", "index")
12
+ end
13
+
14
+ controller do
15
+ def index
16
+ @success = true
17
+ render nothing: true
18
+ end
19
+
20
+ def new; end
21
+ end
22
+
23
+ before do
24
+ login_user current_user
25
+ end
26
+
27
+ context "when user not have authorization" do
28
+ it "redirect to root_path" do
29
+ get :new
30
+ expect(flash[:notice]).to eq "Sem autorização"
31
+ end
32
+ end
33
+
34
+ context "when user have authorization" do
35
+ it "user access the resource" do
36
+ get :index
37
+ expect(assigns(:success)).to be true
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,162 @@
1
+ require "rails_helper"
2
+
3
+ describe Curupira::GroupsController do
4
+ let(:user) { FactoryGirl.create :user }
5
+
6
+ before do
7
+ login_user(user)
8
+ end
9
+
10
+ describe "GET index" do
11
+ before do
12
+ FactoryGirl.create :group
13
+ FactoryGirl.create :group
14
+ end
15
+
16
+ it "should get index" do
17
+ get :index
18
+ expect(response).to be_success
19
+ end
20
+
21
+ it "returns all groups" do
22
+ get :index
23
+ expect(assigns(:groups).count).to eql 2
24
+ end
25
+ end
26
+
27
+ describe "GET show" do
28
+ let(:group) { FactoryGirl.create :group }
29
+
30
+ context "when group exists" do
31
+ it "should get show" do
32
+ get :show, id: group
33
+ expect(response).to be_success
34
+ end
35
+
36
+ it "returns group" do
37
+ get :show, id: group
38
+ expect(assigns(:group)).to eql group
39
+ end
40
+ end
41
+
42
+ context "when group does not exist" do
43
+ it "renders 404" do
44
+ expect {
45
+ get :show, id: "wrong id"
46
+ }.to raise_error ActiveRecord::RecordNotFound
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "GET new" do
52
+ it "should get new" do
53
+ get :new
54
+ expect(response).to be_success
55
+ end
56
+
57
+ it "returns new group" do
58
+ get :new
59
+ expect(assigns(:group)).to be_new_record
60
+ end
61
+ end
62
+
63
+ describe "GET edit" do
64
+ let(:group) { FactoryGirl.create :group }
65
+
66
+ context "when group exists" do
67
+ it "should get edit" do
68
+ get :edit, id: group
69
+ expect(response).to be_success
70
+ end
71
+
72
+ it "returns group" do
73
+ get :edit, id: group
74
+ expect(assigns(:group)).to eql group
75
+ end
76
+ end
77
+
78
+ context "when group does not exist" do
79
+ it "renders 404" do
80
+ expect {
81
+ get :edit, id: "wrong id"
82
+ }.to raise_error ActiveRecord::RecordNotFound
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "POST create" do
88
+ context "when group is valid" do
89
+ let(:params) { { name: "Group Name", active: false } }
90
+
91
+ it "should redirect to new group" do
92
+ post :create, group: params
93
+ expect(flash[:notice]).to eql "Grupo criado com sucesso"
94
+ end
95
+
96
+ it "should redirect to new group" do
97
+ post :create, group: params
98
+ expect(response).to redirect_to assigns(:group)
99
+ end
100
+
101
+ it "creates group" do
102
+ expect {
103
+ post :create, group: params
104
+ }.to change { Group.count }.by(1)
105
+ end
106
+ end
107
+
108
+ context "when group is invalid" do
109
+ let(:params) { { name: "" } }
110
+
111
+ it "does not create group" do
112
+ expect {
113
+ post :create, group: params
114
+ }.to change { Group.count }.by(0)
115
+ end
116
+
117
+ it "should render new" do
118
+ post :create, group: params
119
+ expect(response).to render_template :new
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "PUT update" do
125
+ let(:group) { FactoryGirl.create :group }
126
+
127
+ context "when group is valid" do
128
+ let(:params) { { name: "New group name", active: false } }
129
+
130
+ it "sets flash message" do
131
+ put :update, id: group, group: params
132
+ expect(flash[:notice]).to eql "Grupo atualizado com sucesso"
133
+ end
134
+
135
+ it "redirects to group" do
136
+ put :update, id: group, group: params
137
+ expect(response).to redirect_to assigns(:group)
138
+ end
139
+
140
+ it "updates group" do
141
+ put :update, id: group, group: params
142
+ expect(assigns(:group).name).to eql "New group name"
143
+ expect(assigns(:group).active).to eql false
144
+ end
145
+ end
146
+
147
+ context "when group is invalid" do
148
+ let(:params) { { name: "", active: false } }
149
+
150
+ it "does not create group" do
151
+ put :update, id: group, group: params
152
+ expect(assigns(:group).reload.name).to eql group.name
153
+ expect(assigns(:group).active).to eql true
154
+ end
155
+
156
+ it "should render edit" do
157
+ put :update, id: group, group: params
158
+ expect(response).to render_template :edit
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,171 @@
1
+ require "rails_helper"
2
+
3
+ describe Curupira::PasswordsController do
4
+ describe "GET new" do
5
+ it "should get new" do
6
+ get :new
7
+ expect(response).to be_success
8
+ end
9
+
10
+ it "returns new user" do
11
+ get :new
12
+ expect(assigns(:user)).to be_new_record
13
+ end
14
+
15
+ context "when user is already logged in" do
16
+ it "redirects to root path" do
17
+ login_user(FactoryGirl.create :user)
18
+
19
+ get :new
20
+ expect(response).to redirect_to root_path
21
+ expect(flash[:alert]).to eql "Você já está logado"
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "GET edit" do
27
+ context "when token exists" do
28
+ let!(:user) { FactoryGirl.create :user, reset_password_token: 123456 }
29
+
30
+ it "should get new" do
31
+ get :edit, id: user.reset_password_token
32
+ expect(response).to be_success
33
+ end
34
+
35
+ it "returns user" do
36
+ get :edit, id: user.reset_password_token
37
+ expect(assigns(:user)).to eql user
38
+ end
39
+ end
40
+
41
+ context "when token does not exists" do
42
+ it "redirect to new_session_path" do
43
+ get :edit, id: "no token"
44
+ expect(response).to redirect_to :new_session
45
+ end
46
+
47
+ it "sends flash alert" do
48
+ get :edit, id: "no token"
49
+ expect(flash[:alert]).to eql "Token para resetar senha expirado ou inválido"
50
+ end
51
+ end
52
+
53
+ context "when user is already logged in" do
54
+ it "redirects to root path" do
55
+ login_user(FactoryGirl.create :user)
56
+
57
+ get :edit, id: "any token here"
58
+ expect(response).to redirect_to root_path
59
+ expect(flash[:alert]).to eql "Você já está logado"
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "POST create" do
65
+ context "email exists" do
66
+ let!(:user) { FactoryGirl.create :user }
67
+
68
+ it "delivers password reset instructions" do
69
+ expect {
70
+ post :create, user: { email: user.email }
71
+ }.to change { ActionMailer::Base.deliveries.size }.by(1)
72
+ end
73
+
74
+ it "creates reset password token" do
75
+ expect(user.reload.reset_password_token).to be_nil
76
+
77
+ post :create, user: { email: user.email }
78
+
79
+ expect(user.reload.reset_password_token).to_not be_nil
80
+ end
81
+
82
+ it "redirects to new_session_path" do
83
+ post :create, user: { email: user.email }
84
+ expect(response).to redirect_to new_session_path
85
+ end
86
+
87
+ it "sends flash notice" do
88
+ post :create, user: { email: user.email }
89
+ expect(flash[:notice]).to eql "Verifique seu email para receber instruções de recuperação de senha"
90
+ end
91
+ end
92
+
93
+ context "email does not exists" do
94
+ it "assigns user with wrong email" do
95
+ post :create, user: { email: "wrong@email.com" }
96
+ expect(assigns[:user].email).to eql "wrong@email.com"
97
+ end
98
+
99
+ it "renders new" do
100
+ post :create, user: { email: "wrong@email.com" }
101
+ expect(response).to render_template :new
102
+ end
103
+
104
+ it "sends flash alert" do
105
+ post :create, user: { email: "wrong@email.com" }
106
+ expect(flash[:alert]).to eql "Email não encontrado"
107
+ end
108
+ end
109
+
110
+ context "when user is already logged in" do
111
+ it "redirects to root path" do
112
+ login_user(FactoryGirl.create :user)
113
+
114
+ post :create, id: "any token here"
115
+ expect(response).to redirect_to root_path
116
+ expect(flash[:alert]).to eql "Você já está logado"
117
+ end
118
+ end
119
+ end
120
+
121
+
122
+ describe "PUT update" do
123
+ context "when token exists" do
124
+ let!(:user) { FactoryGirl.create :user, reset_password_token: 123456 }
125
+
126
+ it "redirects to new_session_path" do
127
+ put :update, id: user.reset_password_token, user: { password: "123456" }
128
+ expect(response).to redirect_to new_session_path
129
+ end
130
+
131
+ it "updates users password" do
132
+ expect {
133
+ put :update, id: user.reset_password_token, user: { password: "123456" }
134
+ }.to change { user.reload.crypted_password }
135
+ end
136
+
137
+ it "delivers reseted password email notification" do
138
+ expect {
139
+ put :update, id: user.reset_password_token, user: { password: "123456" }
140
+ }.to change { ActionMailer::Base.deliveries.size }.by(1)
141
+ end
142
+
143
+ it "sends flash notice" do
144
+ put :update, id: user.reset_password_token, user: { password: "123456" }
145
+ expect(flash[:notice]).to eql "Senha atualizada com sucesso. Entre com sua nova senha"
146
+ end
147
+ end
148
+
149
+ context "when token does not exists" do
150
+ it "redirect to new_session_path" do
151
+ put :update, id: "i dont exist"
152
+ expect(response).to redirect_to :new_session
153
+ end
154
+
155
+ it "sends flash alert" do
156
+ put :update, id: "i dont exist"
157
+ expect(flash[:alert]).to eql "Token para resetar senha expirado ou inválido"
158
+ end
159
+ end
160
+
161
+ context "when user is already logged in" do
162
+ it "redirects to root path" do
163
+ login_user(FactoryGirl.create :user)
164
+
165
+ put :update, id: "any token here"
166
+ expect(response).to redirect_to root_path
167
+ expect(flash[:alert]).to eql "Você já está logado"
168
+ end
169
+ end
170
+ end
171
+ end