api_user_auth 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +36 -0
- data/Rakefile +53 -0
- data/app/assets/config/api_user_auth_manifest.js +2 -0
- data/app/assets/javascripts/api_user_auth/application.js +15 -0
- data/app/assets/stylesheets/api_user_auth/application.css +15 -0
- data/app/controllers/api_user_auth/auth_controller.rb +75 -0
- data/app/helpers/api_user_auth/application_helper.rb +5 -0
- data/app/jobs/api_user_auth/application_job.rb +4 -0
- data/app/mailers/api_user_auth/application_mailer.rb +7 -0
- data/app/mailers/api_user_auth/forgot_password_mailer.rb +12 -0
- data/app/mailers/api_user_auth/welcome_mailer.rb +11 -0
- data/app/models/api_user_auth/application_record.rb +5 -0
- data/app/models/api_user_auth/auth_user.rb +167 -0
- data/app/views/api_user_auth/forgot_password_mailer/reset_code.html.erb +3 -0
- data/app/views/api_user_auth/welcome_mailer/welcome.html.erb +2 -0
- data/app/views/layouts/api_user_auth/application.html.erb +16 -0
- data/config/routes.rb +12 -0
- data/db/migrate/20180703111608_create_api_user_auth_auth_users.rb +16 -0
- data/lib/api_user_auth/controller.rb +39 -0
- data/lib/api_user_auth/engine.rb +10 -0
- data/lib/api_user_auth/exceptions.rb +9 -0
- data/lib/api_user_auth/providers/facebook.rb +61 -0
- data/lib/api_user_auth/providers/google.rb +59 -0
- data/lib/api_user_auth/providers/instagram.rb +53 -0
- data/lib/api_user_auth/version.rb +3 -0
- data/lib/api_user_auth.rb +11 -0
- data/lib/generators/api_user_auth_generator.rb +7 -0
- data/lib/tasks/api_user_auth_tasks.rake +4 -0
- data/spec/controllers/api_user_auth/auth_controller_spec.rb +344 -0
- data/spec/controllers/api_user_auth/controller_spec.rb +39 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +4 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/controllers/test_controller.rb +8 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +36 -0
- data/spec/dummy/bin/update +31 -0
- data/spec/dummy/bin/yarn +11 -0
- data/spec/dummy/config/application.rb +30 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +16 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +63 -0
- data/spec/dummy/config/environments/production.rb +89 -0
- data/spec/dummy/config/environments/test.rb +46 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +14 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +25 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +34 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/schema.rb +31 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +507 -0
- data/spec/dummy/log/test.log +42941 -0
- data/spec/dummy/package.json +5 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/models/api_user_auth/auth_user_spec.rb +16 -0
- data/spec/rails_helper.rb +61 -0
- data/spec/spec_helper.rb +93 -0
- data/spec/support/request_spec_helper.rb +13 -0
- metadata +273 -0
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ApiUserAuth
|
4
|
+
module Providers
|
5
|
+
# Get info from Google token
|
6
|
+
class Google
|
7
|
+
API_PATH = 'https://www.googleapis.com/plus/v1/people/me'.freeze
|
8
|
+
|
9
|
+
def initialize(token)
|
10
|
+
@token = token
|
11
|
+
@data = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def api_info_url
|
15
|
+
params = {
|
16
|
+
access_token: @token
|
17
|
+
}
|
18
|
+
URI("#{API_PATH}?#{params.to_query}")
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_user_data
|
22
|
+
api_get_request
|
23
|
+
user_data
|
24
|
+
end
|
25
|
+
|
26
|
+
def user_data
|
27
|
+
{
|
28
|
+
id: @data[:id], name: @data[:displayName],
|
29
|
+
email: @data[:emails].first.try(:[], :value),
|
30
|
+
img_url: (@data[:image] || {}).try(:[], :url),
|
31
|
+
info: {
|
32
|
+
birthday: @data[:birthday],
|
33
|
+
city: (@data[:hometown] || {}).try(:[], :name),
|
34
|
+
gender: @data[:gender]
|
35
|
+
}
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.get_user(token)
|
40
|
+
g = Google.new(token)
|
41
|
+
g.get_user_data
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def api_get_request
|
47
|
+
response = ::Net::HTTP.get_response(api_info_url)
|
48
|
+
case response.code.to_i
|
49
|
+
when 200
|
50
|
+
@data = JSON.parse(response.body, symbolize_names: true)
|
51
|
+
when 400
|
52
|
+
raise ApiUserAuth::Exceptions::InvalidToken, 'Invalid Token'
|
53
|
+
else
|
54
|
+
raise ApiUserAuth::Exceptions::ProviderError, 'Provider Error'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module ApiUserAuth
|
2
|
+
module Providers
|
3
|
+
# Instagram
|
4
|
+
class Instagram
|
5
|
+
API_PATH = 'https://api.instagram.com/v1/users/self/'.freeze
|
6
|
+
|
7
|
+
def initialize(token)
|
8
|
+
@token = token
|
9
|
+
@data = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def api_info_url
|
13
|
+
params = {
|
14
|
+
access_token: @token
|
15
|
+
}
|
16
|
+
URI("#{API_PATH}?#{params.to_query}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_user_data
|
20
|
+
api_get_request
|
21
|
+
user_data
|
22
|
+
end
|
23
|
+
|
24
|
+
def user_data
|
25
|
+
{
|
26
|
+
id: @data[:id], name: @data[:full_name],
|
27
|
+
email: "#{@data[:username]}@instagram.com",
|
28
|
+
img_url: @data[:profile_picture],
|
29
|
+
info: {}
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.get_user(token)
|
34
|
+
inst = Instagram.new(token)
|
35
|
+
inst.get_user_data
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def api_get_request
|
41
|
+
response = ::Net::HTTP.get_response(api_info_url)
|
42
|
+
case response.code.to_i
|
43
|
+
when 200
|
44
|
+
@data = JSON.parse(response.body, symbolize_names: true)[:data]
|
45
|
+
when 400
|
46
|
+
raise ApiUserAuth::Exceptions::InvalidToken, 'Invalid Token'
|
47
|
+
else
|
48
|
+
raise ApiUserAuth::Exceptions::ProviderError, 'Provider Error'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'api_user_auth/engine'
|
2
|
+
require 'api_user_auth/exceptions'
|
3
|
+
require 'api_user_auth/providers/facebook'
|
4
|
+
require 'api_user_auth/providers/google'
|
5
|
+
require 'api_user_auth/providers/instagram'
|
6
|
+
require 'api_user_auth/controller'
|
7
|
+
require 'net/https'
|
8
|
+
|
9
|
+
module ApiUserAuth
|
10
|
+
UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
11
|
+
end
|
@@ -0,0 +1,344 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
module ApiUserAuth
|
4
|
+
RSpec.describe AuthController, type: :controller do
|
5
|
+
routes { ApiUserAuth::Engine.routes }
|
6
|
+
|
7
|
+
context 'Create' do
|
8
|
+
it 'Valid' do
|
9
|
+
expect(AuthUser.count).to eq(0)
|
10
|
+
post :create, params: { email: 'user@mail.com', password: '123456' }
|
11
|
+
expect(AuthUser.count).to eq(1)
|
12
|
+
|
13
|
+
expect(response).to have_http_status(201)
|
14
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
15
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
16
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
17
|
+
expect(resp_json[:is_new]).to be_truthy
|
18
|
+
# expect(AuthUser.count).to eq(1)
|
19
|
+
# post :create, params: { email: 'user@mail.com', password: '123456' }
|
20
|
+
# expect(AuthUser.count).to eq(1)
|
21
|
+
# expect(response).to have_http_status(200)
|
22
|
+
# expect(resp_json[:email]).to eq('user@mail.com')
|
23
|
+
# expect(resp_json[:auth_token]).not_to be_blank
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'Invalid' do
|
27
|
+
expect(AuthUser.count).to eq(0)
|
28
|
+
|
29
|
+
post :create, params: { email: 'user@mail.com' }
|
30
|
+
expect(response).to have_http_status(422)
|
31
|
+
expect(AuthUser.count).to eq(0)
|
32
|
+
expect(resp_json[:message]).not_to be_blank
|
33
|
+
|
34
|
+
post :create, params: { password: '123456' }
|
35
|
+
expect(response).to have_http_status(422)
|
36
|
+
expect(AuthUser.count).to eq(0)
|
37
|
+
expect(resp_json[:message]).not_to be_blank
|
38
|
+
|
39
|
+
post :create, params: { email: 'user@mail.com', password: '' }
|
40
|
+
expect(response).to have_http_status(422)
|
41
|
+
expect(AuthUser.count).to eq(0)
|
42
|
+
expect(resp_json[:message]).not_to be_blank
|
43
|
+
|
44
|
+
post :create, params: { password: '123456', email: '' }
|
45
|
+
expect(response).to have_http_status(422)
|
46
|
+
expect(AuthUser.count).to eq(0)
|
47
|
+
expect(resp_json[:message]).not_to be_blank
|
48
|
+
|
49
|
+
post :create, params: { email: 'user@mail.com', password: '123456' }
|
50
|
+
expect(AuthUser.count).to eq(1)
|
51
|
+
expect(response).to have_http_status(201)
|
52
|
+
|
53
|
+
post :create, params: { email: 'user@mail.com', password: '123456' }
|
54
|
+
expect(AuthUser.count).to eq(1)
|
55
|
+
expect(response).to have_http_status(422)
|
56
|
+
expect(resp_json[:message]).not_to be_blank
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'Login' do
|
61
|
+
before do
|
62
|
+
AuthUser.create_by_params(email: 'user@mail.com', password: '123456')
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'Valid' do
|
66
|
+
expect(AuthUser.count).to eq(1)
|
67
|
+
expect(AuthUser.last.email).to eq('user@mail.com')
|
68
|
+
|
69
|
+
post :login, params: { email: 'user@mail.com', password: '123456' }
|
70
|
+
expect(response).to have_http_status(200)
|
71
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
72
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
73
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'Invalid' do
|
77
|
+
expect(AuthUser.count).to eq(1)
|
78
|
+
expect(AuthUser.last.email).to eq('user@mail.com')
|
79
|
+
|
80
|
+
post :login, params: { email: 'user@mail.com' }
|
81
|
+
expect(response).to have_http_status(422)
|
82
|
+
expect(resp_json[:message]).not_to be_blank
|
83
|
+
|
84
|
+
post :login, params: { password: '123456' }
|
85
|
+
expect(response).to have_http_status(422)
|
86
|
+
expect(resp_json[:message]).not_to be_blank
|
87
|
+
|
88
|
+
post :login, params: { email: 'user2@mail.com', password: '123456' }
|
89
|
+
expect(response).to have_http_status(401)
|
90
|
+
expect(resp_json[:message]).not_to be_blank
|
91
|
+
|
92
|
+
post :login, params: { email: 'user@mail.com', password: '123457' }
|
93
|
+
expect(response).to have_http_status(401)
|
94
|
+
expect(resp_json[:message]).not_to be_blank
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'Password' do
|
99
|
+
let(:auth_user) do
|
100
|
+
AuthUser.create_by_params(email: 'user@mail.com', password: '123456')
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'Valid' do
|
104
|
+
expect(auth_user.valid_password?('123456')).to be_truthy
|
105
|
+
auth_user.update(code: '123456')
|
106
|
+
|
107
|
+
patch :password, params: {
|
108
|
+
email: 'user@mail.com', password: '123457', code: '123456'
|
109
|
+
}
|
110
|
+
expect(response).to have_http_status(200)
|
111
|
+
auth_user.reload
|
112
|
+
expect(auth_user.valid_password?('123456')).to be_falsey
|
113
|
+
expect(auth_user.valid_password?('123457')).to be_truthy
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'Invalid' do
|
117
|
+
expect(auth_user.valid_password?('123456')).to be_truthy
|
118
|
+
auth_user.update(code: '123456')
|
119
|
+
|
120
|
+
patch :password, params: {
|
121
|
+
email: 'user1@mail.com', password: '123457', code: '123456'
|
122
|
+
}
|
123
|
+
expect(response).to have_http_status(422)
|
124
|
+
expect(resp_json[:message]).not_to be_blank
|
125
|
+
|
126
|
+
patch :password, params: {
|
127
|
+
email: 'user@mail.com', password: '123457', code: '123457'
|
128
|
+
}
|
129
|
+
expect(response).to have_http_status(422)
|
130
|
+
expect(resp_json[:message]).not_to be_blank
|
131
|
+
|
132
|
+
patch :password, params: {
|
133
|
+
email: 'user@mail.com', password: '123457'
|
134
|
+
}
|
135
|
+
expect(response).to have_http_status(422)
|
136
|
+
expect(resp_json[:message]).not_to be_blank
|
137
|
+
|
138
|
+
patch :password, params: {
|
139
|
+
code: '123457', password: '123457'
|
140
|
+
}
|
141
|
+
expect(response).to have_http_status(422)
|
142
|
+
expect(resp_json[:message]).not_to be_blank
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'Provider' do
|
147
|
+
context 'Google' do
|
148
|
+
it 'Valid' do
|
149
|
+
allow(ApiUserAuth::Providers::Google).to(
|
150
|
+
receive_message_chain(:get_user).and_return(
|
151
|
+
email: 'user@mail.com', name: 'User Name'
|
152
|
+
)
|
153
|
+
)
|
154
|
+
|
155
|
+
expect(AuthUser.count).to eq(0)
|
156
|
+
post :provider, params: { provider: 'google', token: 'token' }
|
157
|
+
expect(AuthUser.count).to eq(1)
|
158
|
+
expect(response).to have_http_status(200)
|
159
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
160
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
161
|
+
expect(resp_json[:is_new]).to be_truthy
|
162
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
163
|
+
|
164
|
+
post :provider, params: { provider: 'google', token: 'token' }
|
165
|
+
expect(AuthUser.count).to eq(1)
|
166
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
167
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
168
|
+
expect(resp_json[:is_new]).to be_falsey
|
169
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'Invalid' do
|
173
|
+
expect(AuthUser.count).to eq(0)
|
174
|
+
post :provider, params: { provider: 'google', token: 'token' }
|
175
|
+
expect(AuthUser.count).to eq(0)
|
176
|
+
expect(response).to have_http_status(422)
|
177
|
+
expect(resp_json[:message]).not_to be_blank
|
178
|
+
|
179
|
+
post :provider, params: { provider: 'google2', token: 'token' }
|
180
|
+
expect(AuthUser.count).to eq(0)
|
181
|
+
expect(response).to have_http_status(422)
|
182
|
+
expect(resp_json[:message]).not_to be_blank
|
183
|
+
|
184
|
+
post :provider, params: { provider: 'google' }
|
185
|
+
expect(AuthUser.count).to eq(0)
|
186
|
+
expect(response).to have_http_status(422)
|
187
|
+
expect(resp_json[:message]).not_to be_blank
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
context 'Facebook' do
|
192
|
+
it 'Valid' do
|
193
|
+
allow(ApiUserAuth::Providers::Facebook).to(
|
194
|
+
receive_message_chain(:get_user).and_return(
|
195
|
+
email: 'user@mail.com', name: 'User Name'
|
196
|
+
)
|
197
|
+
)
|
198
|
+
expect(AuthUser.count).to eq(0)
|
199
|
+
post :provider, params: { provider: 'facebook', token: 'token' }
|
200
|
+
expect(AuthUser.count).to eq(1)
|
201
|
+
|
202
|
+
expect(response).to have_http_status(200)
|
203
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
204
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
205
|
+
expect(resp_json[:is_new]).to be_truthy
|
206
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
207
|
+
|
208
|
+
post :provider, params: { provider: 'facebook', token: 'token' }
|
209
|
+
expect(AuthUser.count).to eq(1)
|
210
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
211
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
212
|
+
expect(resp_json[:is_new]).to be_falsey
|
213
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'Invalid' do
|
217
|
+
expect(AuthUser.count).to eq(0)
|
218
|
+
post :provider, params: { provider: 'facebook', token: 'token' }
|
219
|
+
expect(AuthUser.count).to eq(0)
|
220
|
+
expect(response).to have_http_status(422)
|
221
|
+
expect(resp_json[:message]).not_to be_blank
|
222
|
+
|
223
|
+
post :provider, params: { provider: 'facebook2', token: 'token' }
|
224
|
+
expect(AuthUser.count).to eq(0)
|
225
|
+
expect(response).to have_http_status(422)
|
226
|
+
expect(resp_json[:message]).not_to be_blank
|
227
|
+
|
228
|
+
post :provider, params: { provider: 'facebook' }
|
229
|
+
expect(AuthUser.count).to eq(0)
|
230
|
+
expect(response).to have_http_status(422)
|
231
|
+
expect(resp_json[:message]).not_to be_blank
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
context 'Instagram' do
|
236
|
+
it 'Valid' do
|
237
|
+
allow(ApiUserAuth::Providers::Instagram).to(
|
238
|
+
receive_message_chain(:get_user).and_return(
|
239
|
+
email: 'user@mail.com', name: 'User Name'
|
240
|
+
)
|
241
|
+
)
|
242
|
+
expect(AuthUser.count).to eq(0)
|
243
|
+
post :provider, params: { provider: 'instagram', token: 'token' }
|
244
|
+
expect(AuthUser.count).to eq(1)
|
245
|
+
|
246
|
+
expect(response).to have_http_status(200)
|
247
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
248
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
249
|
+
expect(resp_json[:is_new]).to be_truthy
|
250
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
251
|
+
|
252
|
+
post :provider, params: { provider: 'instagram', token: 'token' }
|
253
|
+
expect(AuthUser.count).to eq(1)
|
254
|
+
expect(resp_json[:email]).to eq('user@mail.com')
|
255
|
+
expect(resp_json[:auth_token]).not_to be_blank
|
256
|
+
expect(resp_json[:is_new]).to be_falsey
|
257
|
+
expect(resp_json[:id]).to eq(AuthUser.last.id)
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'Invalid' do
|
261
|
+
expect(AuthUser.count).to eq(0)
|
262
|
+
post :provider, params: { provider: 'instagram', token: 'token' }
|
263
|
+
expect(AuthUser.count).to eq(0)
|
264
|
+
expect(response).to have_http_status(422)
|
265
|
+
expect(resp_json[:message]).not_to be_blank
|
266
|
+
|
267
|
+
post :provider, params: { provider: 'instagram2', token: 'token' }
|
268
|
+
expect(AuthUser.count).to eq(0)
|
269
|
+
expect(response).to have_http_status(422)
|
270
|
+
expect(resp_json[:message]).not_to be_blank
|
271
|
+
|
272
|
+
post :provider, params: { provider: 'instagram' }
|
273
|
+
expect(AuthUser.count).to eq(0)
|
274
|
+
expect(response).to have_http_status(422)
|
275
|
+
expect(resp_json[:message]).not_to be_blank
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
context 'Forgot password' do
|
281
|
+
it 'Valid' do
|
282
|
+
auth_user = AuthUser.create_by_params(
|
283
|
+
email: 'user@mail.com', password: '123456'
|
284
|
+
)
|
285
|
+
expect(auth_user.valid_password?('123456')).to be_truthy
|
286
|
+
expect(auth_user.code).to be_blank
|
287
|
+
|
288
|
+
patch :forgot_password, params: {
|
289
|
+
email: 'user@mail.com'
|
290
|
+
}
|
291
|
+
expect(response).to have_http_status(200)
|
292
|
+
auth_user.reload
|
293
|
+
expect(auth_user.code).not_to be_blank
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'Invalid' do
|
297
|
+
auth_user = AuthUser.create_by_params(
|
298
|
+
email: 'user@mail.com', password: '123456'
|
299
|
+
)
|
300
|
+
expect(auth_user.valid_password?('123456')).to be_truthy
|
301
|
+
expect(auth_user.code).to be_blank
|
302
|
+
|
303
|
+
patch :forgot_password, params: {
|
304
|
+
email: 'user1@mail.com'
|
305
|
+
}
|
306
|
+
expect(response).to have_http_status(422)
|
307
|
+
expect(resp_json[:message]).not_to be_blank
|
308
|
+
auth_user.reload
|
309
|
+
expect(auth_user.code).to be_blank
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
context 'Logout' do
|
314
|
+
let(:auth_user) do
|
315
|
+
AuthUser.create_by_params(email: 'user@mail.com', password: '123456')
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'Valid' do
|
319
|
+
expect(auth_user.valid_password?('123456')).to be_truthy
|
320
|
+
expect(auth_user.auth_tokens.count).to eq(1)
|
321
|
+
token = auth_user.auth_tokens.last
|
322
|
+
|
323
|
+
request.headers['HTTP_AUTHORIZATION'] = "Bearer #{token}"
|
324
|
+
delete :logout
|
325
|
+
expect(response).to have_http_status(200)
|
326
|
+
auth_user.reload
|
327
|
+
expect(auth_user.auth_tokens.count).to eq(0)
|
328
|
+
end
|
329
|
+
|
330
|
+
it 'Invalid' do
|
331
|
+
expect(auth_user.valid_password?('123456')).to be_truthy
|
332
|
+
expect(auth_user.auth_tokens.count).to eq(1)
|
333
|
+
token = auth_user.auth_tokens.last
|
334
|
+
|
335
|
+
request.headers['HTTP_AUTHORIZATION'] = "Bearer #{SecureRandom.uuid}"
|
336
|
+
delete :logout
|
337
|
+
expect(response).to have_http_status(400)
|
338
|
+
auth_user.reload
|
339
|
+
expect(auth_user.auth_tokens.count).to eq(1)
|
340
|
+
end
|
341
|
+
|
342
|
+
end
|
343
|
+
end
|
344
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
module ApiUserAuth
|
4
|
+
RSpec.describe TestController, type: :controller do
|
5
|
+
context 'Test functional' do
|
6
|
+
before(:example) do
|
7
|
+
AuthUser.create_by_params(
|
8
|
+
email: 'user@mail.com', password: '123456'
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:auth_user) { AuthUser.last }
|
13
|
+
|
14
|
+
it 'Valid' do
|
15
|
+
expect(AuthUser.count).to eq(1)
|
16
|
+
request.headers.merge!({ 'HTTP_AUTHORIZATION' => "Bearer #{auth_user.auth_tokens.last}" })
|
17
|
+
get :index
|
18
|
+
expect(response).to have_http_status(200)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'Invalid' do
|
22
|
+
expect(AuthUser.count).to eq(1)
|
23
|
+
get :index
|
24
|
+
expect(response).to have_http_status(401)
|
25
|
+
expect(resp_json[:message]).to_not be_blank
|
26
|
+
|
27
|
+
request.headers.merge!({ 'HTTP_AUTHORIZATION' => 'Bearer token_goes_here' })
|
28
|
+
get :index
|
29
|
+
expect(response).to have_http_status(401)
|
30
|
+
expect(resp_json[:message]).to_not be_blank
|
31
|
+
|
32
|
+
request.headers.merge!({ 'HTTP_AUTHORIZATION' => "Bearer #{SecureRandom.uuid}" })
|
33
|
+
get :index
|
34
|
+
expect(response).to have_http_status(401)
|
35
|
+
expect(resp_json[:message]).to_not be_blank
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require rails-ujs
|
14
|
+
//= require activestorage
|
15
|
+
//= require_tree
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
<%= csp_meta_tag %>
|
7
|
+
|
8
|
+
<%= stylesheet_link_tag 'application', media: 'all' %>
|
9
|
+
<%= javascript_include_tag 'application' %>
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
<%= yield %>
|
14
|
+
</body>
|
15
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|
data/spec/dummy/bin/rake
ADDED