omniauth-applicaster 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfb4391ae209e39c4f8696aa9739272c001d869b
4
- data.tar.gz: e1bacf27810817e764814c017987dfda3c2c06b6
3
+ metadata.gz: 4d0a3317532707cf06cf526f9fd1415f42192319
4
+ data.tar.gz: 5d82821d7a9f287c4323992430277ce271eba424
5
5
  SHA512:
6
- metadata.gz: 58764a8664b29e35dc140db06f17777fe3c813ef961b6033d8239c1723ec4cbc7b394f1dd89b0ca3bbaca5ceb74d9f521266575638692dd4bfe68b027b1502ce
7
- data.tar.gz: 955089493db46cba1ae86a5b59849ede304c9ea12b43a4c4cc5e504187fe7cd47f86976badf489b8963910645e1352f525a5d7f86a2fd186b139754dbf6bb92e
6
+ metadata.gz: bb5b8fbcdad31b8183625d25720d07645eacc6bc17a67147ad9d7238665e3dab674a9d25166a428525bdfb19e529624d3b8cc5e7fc5bf58529c472acc589e08b
7
+ data.tar.gz: 044fd3eade0aba820f4ef97b91bf1b2b5dd1492d04f81e267b8c859dc531a4360f38d87eaa4c72559cc08684fb65933a42d101cfa25f75db6e60d9baaa3ab6f3
@@ -55,6 +55,12 @@ module Applicaster
55
55
  .get("/api/v1/users/current.json")
56
56
  .body
57
57
  )
58
+ rescue Faraday::ClientError => e
59
+ if e.response[:status] == 401
60
+ nil
61
+ else
62
+ raise e
63
+ end
58
64
  end
59
65
  end
60
66
 
@@ -1,5 +1,3 @@
1
- require_relative "user"
2
-
3
1
  module Applicaster
4
2
  module AuthHelpers
5
3
  def current_user
@@ -23,13 +21,8 @@ module Applicaster
23
21
  return nil unless session[:omniauth_credentials]
24
22
 
25
23
  token = session[:omniauth_credentials][:token]
26
- Applicaster::Accounts.user_from_token(token)
27
- rescue Faraday::ClientError => e
28
- if e.response[:status] == 401
29
- session.delete(:omniauth_credentials)
30
- nil
31
- else
32
- raise e
24
+ Applicaster::Accounts.user_from_token(token).tap do |user|
25
+ session.delete(:omniauth_credentials) unless user
33
26
  end
34
27
  end
35
28
  end
@@ -1,5 +1,5 @@
1
1
  module Omniauth
2
2
  module Applicaster
3
- VERSION = "1.1.0"
3
+ VERSION = "1.1.1"
4
4
  end
5
5
  end
@@ -33,56 +33,6 @@ RSpec.describe Applicaster::Accounts do
33
33
  end
34
34
  end
35
35
 
36
- describe "#initialize" do
37
- it "accepts client_id and client_secret" do
38
- service = Applicaster::Accounts.new("my_client_id", "my_client_secret")
39
-
40
- expect(service.client_id).to eq("my_client_id")
41
- expect(service.client_secret).to eq("my_client_secret")
42
- end
43
-
44
- it "takes default values from ENV vars" do
45
- expect(accounts_service.client_id).to eq("client_id")
46
- expect(accounts_service.client_secret).to eq("client_secret")
47
- end
48
- end
49
-
50
- describe "#accounts" do
51
- before do
52
- stub_client_credentials_request
53
- stub_accounts_index_request
54
- end
55
-
56
- it "returns an array of Account objects" do
57
- expect(return_value).to be_kind_of(Array)
58
- expect(return_value.size).to eq(2)
59
- expect(return_value.first).to be_kind_of(Applicaster::Accounts::Account)
60
- end
61
-
62
- def return_value
63
- @return_value ||= accounts_service.accounts
64
- end
65
-
66
- def stub_accounts_index_request
67
- stub_request(:get, "https://accounts2.applicaster.com/api/v1/accounts.json").
68
- with(query: { access_token: "client-credentials-token" }).
69
- to_return(successful_json_response(mock_accounts_response))
70
- end
71
-
72
- def mock_accounts_response
73
- [
74
- {
75
- id: "1-account-1",
76
- name: "Account 1",
77
- },
78
- {
79
- id: "2-account-2",
80
- name: "Account 2",
81
- },
82
- ]
83
- end
84
- end
85
-
86
36
  describe ".connection" do
87
37
  let(:remote_url) { "https://accounts2.applicaster.com/test.json" }
88
38
  let(:request_stub) { stub_request(:get, remote_url) }
@@ -137,6 +87,80 @@ RSpec.describe Applicaster::Accounts do
137
87
  end
138
88
  end
139
89
 
90
+ describe ".user_from_token" do
91
+ let(:return_value) { Applicaster::Accounts.user_from_token(token) }
92
+
93
+ before do
94
+ stub_current_user_requests
95
+ end
96
+
97
+ context "when token is valid" do
98
+ let(:token) { "valid-access-token" }
99
+
100
+ it "returns an Applicaster::Accounts::User instance" do
101
+ expect(return_value).to be_kind_of(Applicaster::Accounts::User)
102
+ end
103
+ end
104
+
105
+ context "when token is invalid" do
106
+ let(:token) { "invalid-access-token" }
107
+
108
+ it "returns nil" do
109
+ expect(return_value).to be nil
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "#initialize" do
115
+ it "accepts client_id and client_secret" do
116
+ service = Applicaster::Accounts.new("my_client_id", "my_client_secret")
117
+
118
+ expect(service.client_id).to eq("my_client_id")
119
+ expect(service.client_secret).to eq("my_client_secret")
120
+ end
121
+
122
+ it "takes default values from ENV vars" do
123
+ expect(accounts_service.client_id).to eq("client_id")
124
+ expect(accounts_service.client_secret).to eq("client_secret")
125
+ end
126
+ end
127
+
128
+ describe "#accounts" do
129
+ before do
130
+ stub_client_credentials_request
131
+ stub_accounts_index_request
132
+ end
133
+
134
+ it "returns an array of Account objects" do
135
+ expect(return_value).to be_kind_of(Array)
136
+ expect(return_value.size).to eq(2)
137
+ expect(return_value.first).to be_kind_of(Applicaster::Accounts::Account)
138
+ end
139
+
140
+ def return_value
141
+ @return_value ||= accounts_service.accounts
142
+ end
143
+
144
+ def stub_accounts_index_request
145
+ stub_request(:get, "https://accounts2.applicaster.com/api/v1/accounts.json").
146
+ with(query: { access_token: "client-credentials-token" }).
147
+ to_return(successful_json_response(mock_accounts_response))
148
+ end
149
+
150
+ def mock_accounts_response
151
+ [
152
+ {
153
+ id: "1-account-1",
154
+ name: "Account 1",
155
+ },
156
+ {
157
+ id: "2-account-2",
158
+ name: "Account 2",
159
+ },
160
+ ]
161
+ end
162
+ end
163
+
140
164
  def with_base_url(url)
141
165
  value_bofre, ENV["ACCOUNTS_BASE_URL"] = ENV["ACCOUNTS_BASE_URL"], url
142
166
  yield
@@ -99,20 +99,4 @@ RSpec.describe Applicaster::AuthHelpers do
99
99
  }
100
100
  }
101
101
  end
102
-
103
- def stub_current_user_requests
104
- stub_request(:get, "https://accounts2.applicaster.com/api/v1/users/current.json")
105
- .with(query: { access_token: "valid-access-token" })
106
- .to_return(successful_json_response(mock_user_response))
107
-
108
- stub_request(:get, "https://accounts2.applicaster.com/api/v1/users/current.json")
109
- .with(query: { access_token: "invalid-access-token" })
110
- .to_return(status: 401, body: "")
111
- end
112
-
113
- def mock_user_response
114
- {
115
- id: "123"
116
- }
117
- end
118
102
  end
@@ -4,18 +4,38 @@ module WebmockStubsHelper
4
4
  end
5
5
 
6
6
  def stub_client_credentials_request
7
- stub_request(:post, "https://client_id:client_secret@accounts2.applicaster.com/oauth/token").
8
- with(:body => {"grant_type"=>"client_credentials"}).
9
- to_return(successful_json_response(access_token: "client-credentials-token"))
7
+ stub_request(:post, "https://client_id:client_secret@#{accounts_host}/oauth/token")
8
+ .with(:body => {"grant_type"=>"client_credentials"})
9
+ .to_return(successful_json_response(access_token: "client-credentials-token"))
10
10
  end
11
11
 
12
- def successful_json_response(body)
13
- {
14
- status: 200,
15
- body: body.to_json,
16
- headers: {
17
- "Content-Type" => "application/json"
18
- }
12
+ def stub_current_user_requests
13
+ stub_request(:get, "https://#{accounts_host}/api/v1/users/current.json")
14
+ .with(query: { access_token: "valid-access-token" })
15
+ .to_return(successful_json_response(mock_user_response))
16
+
17
+ stub_request(:get, "https://#{accounts_host}/api/v1/users/current.json")
18
+ .with(query: { access_token: "invalid-access-token" })
19
+ .to_return(status: 401, body: "")
20
+ end
21
+
22
+ def accounts_host
23
+ "accounts2.applicaster.com"
24
+ end
25
+
26
+ def mock_user_response
27
+ {
28
+ id: "123"
29
+ }
30
+ end
31
+
32
+ def successful_json_response(body)
33
+ {
34
+ status: 200,
35
+ body: body.to_json,
36
+ headers: {
37
+ "Content-Type" => "application/json"
19
38
  }
20
- end
39
+ }
40
+ end
21
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-applicaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neer Friedman
@@ -140,7 +140,6 @@ files:
140
140
  - lib/applicaster/accounts/user.rb
141
141
  - lib/applicaster/auth_helpers.rb
142
142
  - lib/applicaster/sessions_controller_mixin.rb
143
- - lib/applicaster/user.rb
144
143
  - lib/omniauth-applicaster.rb
145
144
  - lib/omniauth-applicaster/version.rb
146
145
  - lib/omniauth/strategies/applicaster.rb
@@ -1,37 +0,0 @@
1
- module Applicaster
2
- class User
3
- attr_accessor :user_json
4
-
5
- def initialize(user_json)
6
- @user_json = user_json.symbolize_keys
7
- end
8
-
9
- def id
10
- user_json[:id]
11
- end
12
-
13
- def name
14
- user_json[:name]
15
- end
16
-
17
- def email
18
- user_json[:email]
19
- end
20
-
21
- def global_roles
22
- user_json[:global_roles]
23
- end
24
-
25
- def permissions
26
- user_json[:permissions]
27
- end
28
-
29
- def admin
30
- user_json[:admin]
31
- end
32
-
33
- def admin?
34
- !!admin
35
- end
36
- end
37
- end