omniauth-microsoft-office365 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c5f03607220a87567e619bb32d66cfb18e837ed
4
- data.tar.gz: 23f08b52c77deb9ed8bbbde09fc92a29fd47c9ee
3
+ metadata.gz: 84463d49baf7efb41ce48a2c7bb69f941a4dd79e
4
+ data.tar.gz: be291be2bc42901ff687ce87478ea22f2a5b0f7c
5
5
  SHA512:
6
- metadata.gz: 2c7fb3a7b86c5d2bb1359547bed0e9ad4439b809b7354ad3291beb731eca6027771fecdc590b4d755a1ef4ce209bab4518a4ca976243cb93bbd0b1d8db2b9dae
7
- data.tar.gz: e7d9a20ffa388edd782314f6677c02bbee4606b2844878cb125f7362cad2cc425b737cf8ccccc8763e24b96271173c0a167a871377c09ed7c2084f57839b8c3f
6
+ metadata.gz: dc5a8b18a576f647ecdeeebe67630557bb4426c588cc3b31bb8864351df13a1f16a9ba3e7fa054f957f542b4707096ce88993f277869918f0fc079ce4484f9c9
7
+ data.tar.gz: dc6e1a7de0a9844b3939bbb5cd0b98abc142158dc4bed889d7fc9ac858fcae9f60160b681fd4a6e0ff3a8b3e469c357d3ed5583cc0bc710b51a981598a030117
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module MicrosoftOffice365
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -23,6 +23,7 @@ module OmniAuth
23
23
  display_name: raw_info["DisplayName"],
24
24
  first_name: raw_info["DisplayName"].split(", ")[1],
25
25
  last_name: raw_info["DisplayName"].split(", ")[0],
26
+ image: avatar_file,
26
27
  alias: raw_info["Alias"]
27
28
  }
28
29
  end
@@ -36,6 +37,27 @@ module OmniAuth
36
37
  def raw_info
37
38
  @raw_info ||= access_token.get("https://outlook.office.com/api/v2.0/me/").parsed
38
39
  end
40
+
41
+ private
42
+
43
+ def avatar_file
44
+ photo = access_token.get("https://outlook.office.com/api/v2.0/me/photo/$value")
45
+ ext = photo.content_type.sub("image/", "") # "image/jpeg" => "jpeg"
46
+
47
+ Tempfile.new(["avatar", ".#{ext}"]).tap do |file|
48
+ file.binmode
49
+ file.write(photo.body)
50
+ file.rewind
51
+ end
52
+
53
+ rescue ::OAuth2::Error => e
54
+ if e.response.status == 404 # User has no avatar...
55
+ return nil
56
+ else
57
+ raise
58
+ end
59
+ end
60
+
39
61
  end
40
62
  end
41
63
  end
@@ -1,8 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe OmniAuth::Strategies::MicrosoftOffice365 do
4
- let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) }
5
- let(:options) { { } }
4
+ let(:request) { double("Request", params: {}, cookies: {}, env: {}) }
5
+ let(:access_token) { instance_double(OAuth2::AccessToken) }
6
+ let(:options) { { } }
6
7
 
7
8
  let(:app) do
8
9
  lambda do
@@ -11,12 +12,13 @@ RSpec.describe OmniAuth::Strategies::MicrosoftOffice365 do
11
12
  end
12
13
 
13
14
  let(:strategy) do
14
- OmniAuth::Strategies::MicrosoftOffice365.new(app, 'appid', 'secret', options)
15
+ OmniAuth::Strategies::MicrosoftOffice365.new(app, "appid", "secret", options)
15
16
  end
16
17
 
17
18
  before do
18
19
  OmniAuth.config.test_mode = true
19
20
  allow(strategy).to receive(:request).and_return(request)
21
+ allow(strategy).to receive(:access_token).and_return(access_token)
20
22
  end
21
23
 
22
24
  after do
@@ -48,9 +50,9 @@ RSpec.describe OmniAuth::Strategies::MicrosoftOffice365 do
48
50
  let(:options) do
49
51
  {
50
52
  client_options: {
51
- 'site' => 'https://example.com',
52
- 'authorize_url' => 'https://example.com/authorize',
53
- 'token_url' => 'https://example.com/token',
53
+ "site" => "https://example.com",
54
+ "authorize_url" => "https://example.com/authorize",
55
+ "token_url" => "https://example.com/token",
54
56
  }
55
57
  }
56
58
  end
@@ -84,4 +86,70 @@ RSpec.describe OmniAuth::Strategies::MicrosoftOffice365 do
84
86
  end
85
87
  end
86
88
 
89
+ describe "#info" do
90
+ let(:profile_response) do
91
+ instance_double(OAuth2::Response, parsed: {
92
+ "@odata.context" => "https://outlook.office.com/api/v2.0/$metadata#Me",
93
+ "@odata.id" => "https://outlook.office.com/api/v2.0/Users('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX@XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')",
94
+ "Id" => "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX@XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
95
+ "EmailAddress" => "luke.skywalker@example.com",
96
+ "DisplayName" => "Skywalker, Luke",
97
+ "Alias" => "luke.skywalker",
98
+ "MailboxGuid" => "YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY"
99
+ })
100
+ end
101
+
102
+ before do
103
+ expect(access_token).to receive(:get).with("https://outlook.office.com/api/v2.0/me/")
104
+ .and_return(profile_response)
105
+ end
106
+
107
+ context "when user provided avatar image" do
108
+ let(:avatar_response) { instance_double(OAuth2::Response, content_type: "image/jpeg", body: "JPEG_STREAM") }
109
+
110
+ before do
111
+ expect(access_token).to receive(:get).with("https://outlook.office.com/api/v2.0/me/photo/$value")
112
+ .and_return(avatar_response)
113
+ end
114
+
115
+ it "returns a hash containing normalized user data" do
116
+ expect(strategy.info).to match({
117
+ alias: "luke.skywalker",
118
+ display_name: "Skywalker, Luke",
119
+ email: "luke.skywalker@example.com",
120
+ first_name: "Luke",
121
+ last_name: "Skywalker",
122
+ image: Tempfile,
123
+ })
124
+ end
125
+
126
+ it "downloads avatar to a local file with appropriate extension" do
127
+ avatar = strategy.info[:image]
128
+ expect(avatar.binmode?).to be_truthy
129
+ expect(avatar.path).to match(/avatar.*\.jpeg\z/)
130
+ expect(avatar.read).to eq("JPEG_STREAM")
131
+ end
132
+ end
133
+
134
+ context "when user didn't provide avatar image" do
135
+ let(:avatar_response) { instance_double(OAuth2::Response, status: 404, "error=": nil, parsed: {}, body: '') }
136
+
137
+ before do
138
+ expect(access_token).to receive(:get).with("https://outlook.office.com/api/v2.0/me/photo/$value")
139
+ .and_raise(OAuth2::Error, avatar_response)
140
+ end
141
+
142
+ it "returns a hash containing normalized user data" do
143
+ expect(strategy.info).to match({
144
+ alias: "luke.skywalker",
145
+ display_name: "Skywalker, Luke",
146
+ email: "luke.skywalker@example.com",
147
+ first_name: "Luke",
148
+ last_name: "Skywalker",
149
+ image: nil,
150
+ })
151
+ end
152
+ end
153
+ end
154
+
87
155
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-microsoft-office365
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Urbański
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-30 00:00:00.000000000 Z
11
+ date: 2016-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth