passaporteweb-client 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe PassaporteWeb::IdentityServiceAccount do
5
+ let(:identity) { PassaporteWeb::Identity.find('5e32f927-c4ab-404e-a91c-b2abc05afb56') }
6
+
7
+ describe ".new", vcr: true do
8
+ it "should instanciate a new object with attributes set" do
9
+ attributes = {
10
+ plan_slug: 'basic',
11
+ roles: ['user', 'admin'],
12
+ expiration: '2014-05-01 00:00:00',
13
+ url: '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/',
14
+ membership_details_url: '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/5e32f927-c4ab-404e-a91c-b2abc05afb56/',
15
+ add_member_url: '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/',
16
+ service_data: {"name" => "Identity Client","slug" => "identity_client"},
17
+ account_data: {"name" => "Investimentos","uuid" => "859d3542-84d6-4909-b1bd-4f43c1312065"},
18
+ name: 'Investimentos',
19
+ uuid: '859d3542-84d6-4909-b1bd-4f43c1312065'
20
+ }
21
+ account = described_class.new(identity, attributes)
22
+
23
+ account.should be_instance_of(described_class)
24
+ account.should_not be_persisted
25
+ account.errors.should be_empty
26
+ account.identity.should == identity
27
+
28
+ account.membership_details_url.should == '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/5e32f927-c4ab-404e-a91c-b2abc05afb56/'
29
+ account.plan_slug.should == 'basic'
30
+ account.roles.should == ['user', 'admin']
31
+ account.url.should == '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/'
32
+ account.expiration.should == '2014-05-01 00:00:00'
33
+ account.service_data.should == {"name" => "Identity Client","slug" => "identity_client"}
34
+ account.account_data.should == {"name" => "Investimentos","uuid" => "859d3542-84d6-4909-b1bd-4f43c1312065"}
35
+ account.add_member_url.should == '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/'
36
+ account.name.should == 'Investimentos' # TODO ???
37
+ account.uuid.should == '859d3542-84d6-4909-b1bd-4f43c1312065' # TODO ???
38
+ end
39
+ end
40
+
41
+ describe ".find_all", vcr: true do
42
+ it "should find all service accounts the identity is associted to in the current authenticated application" do
43
+ accounts = described_class.find_all(identity)
44
+ accounts.size.should == 9
45
+ accounts.map { |a| a.instance_of?(described_class) }.uniq.should == [true]
46
+ accounts.map { |a| a.persisted? }.uniq.should == [true]
47
+
48
+ account = accounts.first
49
+ account.errors.should be_empty
50
+ account.identity.should == identity
51
+ account.membership_details_url.should == '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/5e32f927-c4ab-404e-a91c-b2abc05afb56/'
52
+ account.plan_slug.should == 'basic'
53
+ account.roles.should == ['owner', 'foo,bar']
54
+ account.url.should == '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/'
55
+ account.expiration.should == '2014-05-01 00:00:00'
56
+ account.service_data.should == {"name" => "Identity Client","slug" => "identity_client"}
57
+ account.account_data.should == {"name" => "Investimentos","uuid" => "859d3542-84d6-4909-b1bd-4f43c1312065"}
58
+ account.add_member_url.should == '/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/'
59
+ # account.name.should == 'Investimentos' # TODO ???
60
+ # account.uuid.should == '859d3542-84d6-4909-b1bd-4f43c1312065' # TODO ???
61
+ end
62
+ it "should include expired service accounts if asked to" do
63
+ accounts = described_class.find_all(identity, true)
64
+ accounts.size.should == 10
65
+ accounts.map { |a| a.expiration }.uniq.should == ["2014-05-01 00:00:00", nil, "2013-04-02 00:00:00"]
66
+ end
67
+ it "should find only service accounts in which the identity has the supplied role" do
68
+ role = 'foo,bar'
69
+ accounts = described_class.find_all(identity, false, role)
70
+ accounts.size.should == 1
71
+ accounts.map { |a| a.roles.include?(role) }.uniq.should == [true]
72
+ end
73
+ end
74
+
75
+ describe "#save", vcr: true do
76
+ context "on success" do
77
+ it "should create a new service account by name for the identity in the current authenticated application" do
78
+ expiration_date = (Time.now + (15 * 60 * 60 * 24)).strftime('%Y-%m-%d')
79
+ attributes = {
80
+ plan_slug: 'basic',
81
+ expiration: expiration_date,
82
+ name: 'Conta Nova em Folha'
83
+ }
84
+ account = described_class.new(identity, attributes)
85
+ account.save.should be_true
86
+ account.should be_persisted
87
+
88
+ account.plan_slug.should == 'basic'
89
+ account.roles.should == ['owner']
90
+ account.service_data.should == {"name" => "Identity Client","slug" => "identity_client"}
91
+ account.account_data['name'].should == 'Conta Nova em Folha'
92
+ account.account_data['uuid'].should_not be_nil
93
+ account.url.should_not be_nil
94
+ account.membership_details_url.should_not be_nil
95
+ account.add_member_url.should_not be_nil
96
+ end
97
+ it "should create a new service account by uuid for the identity in the current authenticated application" do
98
+ pending 'pegar explicação com Vitor'
99
+ account_uuid = '92d52d25-c7a6-4d16-ae9e-c5f2b4f8fa43'
100
+ expiration_date = (Time.now + (15 * 60 * 60 * 24)).strftime('%Y-%m-%d')
101
+ attributes = {
102
+ plan_slug: 'basic',
103
+ expiration: expiration_date,
104
+ uuid: account_uuid
105
+ }
106
+ account = described_class.new(identity, attributes)
107
+ account.save.should be_true
108
+ account.should be_persisted
109
+
110
+ account.plan_slug.should == 'basic'
111
+ account.roles.should == ['owner']
112
+ account.service_data.should == {"name" => "Identity Client","slug" => "identity_client"}
113
+ account.account_data['name'].should_not be_nil
114
+ account.account_data['uuid'].should == account_uuid
115
+ account.url.should_not be_nil
116
+ account.membership_details_url.should_not be_nil
117
+ account.add_member_url.should_not be_nil
118
+ end
119
+ end
120
+ context "on failure" do
121
+ it "should return false and populate #errors with the failure reasons" do
122
+ expiration_date = (Time.now - (15 * 60 * 60 * 24)).strftime('%Y-%m-%d')
123
+ attributes = {
124
+ plan_slug: 'basic',
125
+ expiration: expiration_date,
126
+ name: 'Conta Nova em Folha 2: A missão'
127
+ }
128
+ account = described_class.new(identity, attributes)
129
+ account.save.should be_false
130
+ account.should_not be_persisted
131
+ account.errors.should == {"field_errors"=>{"expiration"=>["Cannot set the expiration to the past."]}}
132
+ end
133
+ end
134
+ end
135
+
136
+ end
@@ -0,0 +1,290 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe PassaporteWeb::Identity do
5
+
6
+ describe "constants" do
7
+ it { described_class::ATTRIBUTES.should == [:accounts, :birth_date, :country, :cpf, :email, :first_name, :gender, :is_active, :language, :last_name, :nickname, :notifications, :send_myfreecomm_news, :send_partner_news, :services, :timezone, :update_info_url, :uuid, :password, :password2, :must_change_password, :inhibit_activation_message, :tos] }
8
+ it { described_class::UPDATABLE_ATTRIBUTES.should == [:first_name, :last_name, :nickname, :cpf, :birth_date, :gender, :send_myfreecomm_news, :send_partner_news, :country, :language, :timezone] }
9
+ it { described_class::CREATABLE_ATTRIBUTES.should == [:first_name, :last_name, :nickname, :cpf, :birth_date, :gender, :send_myfreecomm_news, :send_partner_news, :country, :language, :timezone, :email, :password, :password2, :must_change_password, :tos] }
10
+ end
11
+
12
+ describe ".new" do
13
+ it "should instanciate an empty object" do
14
+ identity = described_class.new
15
+ identity.attributes.should == {:accounts=>nil, :birth_date=>nil, :country=>nil, :cpf=>nil, :email=>nil, :first_name=>nil, :gender=>nil, :is_active=>nil, :language=>nil, :last_name=>nil, :nickname=>nil, :notifications=>nil, :send_myfreecomm_news=>nil, :send_partner_news=>nil, :services=>nil, :timezone=>nil, :update_info_url=>nil, :uuid=>nil, :password=>nil, :password2=>nil, :must_change_password=>nil, :inhibit_activation_message=>nil, :tos=>nil}
16
+ end
17
+ it "should instanciate an object with attributes set" do
18
+ attributes = {
19
+ "last_name" => "da Silva",
20
+ "is_active" => true,
21
+ "timezone" => "GMT-3",
22
+ "nickname" => "Lula",
23
+ "first_name" => "Luis Inácio",
24
+ "send_partner_news" => false,
25
+ "uuid" => "a5868d14-6529-477a-9c6b-a09dd42a7cd2",
26
+ "language" => "pt_BR",
27
+ "country" => "Brasil",
28
+ "update_info_url" => "/profile/api/info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/",
29
+ "send_myfreecomm_news" => false,
30
+ "gender" => "M",
31
+ "birth_date" => "1945-10-27",
32
+ "email" => "lula@example.com",
33
+ "notifications" => {
34
+ "count" => 0,
35
+ "list" => "/notifications/api/"
36
+ },
37
+ "accounts" => [],
38
+ "services" => {
39
+ "myfinance" => "/accounts/api/service-info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/myfinance/",
40
+ "account_manager" => "/accounts/api/service-info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/account_manager/"
41
+ },
42
+ "password"=>nil,
43
+ "password2"=>nil,
44
+ "must_change_password"=>nil,
45
+ "inhibit_activation_message"=>nil,
46
+ "tos"=>nil
47
+ }
48
+ identity = described_class.new(attributes)
49
+ identity.attributes.should == {:accounts=>[], :birth_date=>"1945-10-27", :country=>"Brasil", :cpf=>nil, :email=>"lula@example.com", :first_name=>"Luis Inácio", :gender=>"M", :is_active=>true, :language=>"pt_BR", :last_name=>"da Silva", :nickname=>"Lula", :notifications=>{"count"=>0, "list"=>"/notifications/api/"}, :send_myfreecomm_news=>false, :send_partner_news=>false, :services=>{"myfinance"=>"/accounts/api/service-info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/myfinance/", "account_manager"=>"/accounts/api/service-info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/account_manager/"}, :timezone=>"GMT-3", :update_info_url=>"/profile/api/info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/", :uuid=>"a5868d14-6529-477a-9c6b-a09dd42a7cd2", :password=>nil, :password2=>nil, :must_change_password=>nil, :inhibit_activation_message=>nil, :tos=>nil}
50
+ identity.last_name.should == "da Silva"
51
+ identity.is_active.should == true
52
+ identity.timezone.should == "GMT-3"
53
+ identity.nickname.should == "Lula"
54
+ identity.first_name.should == "Luis Inácio"
55
+ identity.send_partner_news.should == false
56
+ identity.uuid.should == "a5868d14-6529-477a-9c6b-a09dd42a7cd2"
57
+ identity.language.should == "pt_BR"
58
+ identity.country.should == "Brasil"
59
+ identity.update_info_url.should == "/profile/api/info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/"
60
+ identity.send_myfreecomm_news.should == false
61
+ identity.gender.should == "M"
62
+ identity.birth_date.should == "1945-10-27"
63
+ identity.email.should == "lula@example.com"
64
+ identity.notifications.should == {"count" => 0, "list" => "/notifications/api/"}
65
+ identity.accounts.should == []
66
+ identity.services.should == {"myfinance" => "/accounts/api/service-info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/myfinance/", "account_manager" => "/accounts/api/service-info/a5868d14-6529-477a-9c6b-a09dd42a7cd2/account_manager/"}
67
+ identity.password.should be_nil
68
+ identity.password2.should be_nil
69
+ identity.must_change_password.should be_nil
70
+ identity.inhibit_activation_message.should be_nil
71
+ identity.tos.should be_nil
72
+ end
73
+ end
74
+
75
+ describe "#== and #===" do
76
+ it "should identify two profiles with the same uuid as equal" do
77
+ p1 = described_class.new('uuid' => 'some-uuid')
78
+ p2 = described_class.new('uuid' => 'some-uuid')
79
+ p1.should == p2
80
+ p1.should_not === p2
81
+ p2.should == p1
82
+ p2.should_not === p1
83
+ end
84
+ it "should identify two profiles with different uuids as different" do
85
+ p1 = described_class.new('uuid' => 'some-uuid-1')
86
+ p2 = described_class.new('uuid' => 'some-uuid-2')
87
+ p1.should_not == p2
88
+ p1.should_not === p2
89
+ p2.should_not == p1
90
+ p2.should_not === p1
91
+ p3 = described_class.new
92
+ p1.should_not == p3
93
+ p3.should_not == p1
94
+ p3.should_not === p1
95
+ end
96
+ end
97
+
98
+ describe ".find", :vcr => true do
99
+ it "should find the requested profile by uuid" do
100
+ identity = described_class.find("5e32f927-c4ab-404e-a91c-b2abc05afb56")
101
+ identity.should be_instance_of(described_class)
102
+ identity.uuid.should == '5e32f927-c4ab-404e-a91c-b2abc05afb56'
103
+ identity.should be_persisted
104
+ identity.email.should == 'teste@teste.com'
105
+ identity.update_info_url.should == '/accounts/api/identities/5e32f927-c4ab-404e-a91c-b2abc05afb56/'
106
+ identity.accounts.size.should == 9
107
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2014-05-01 00:00:00"]
108
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] rescue nil }.sort.should == [nil]
109
+ end
110
+ it "should find the requested profile by uuid, including expired accounts" do
111
+ identity = described_class.find("5e32f927-c4ab-404e-a91c-b2abc05afb56", true)
112
+ identity.accounts.size.should == 10
113
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2013-04-02 00:00:00", "2014-05-01 00:00:00"]
114
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] rescue nil }.sort.should == [nil]
115
+ end
116
+ it "should find the requested profile by uuid, including other services" do
117
+ identity = described_class.find("5e32f927-c4ab-404e-a91c-b2abc05afb56", false, true)
118
+ identity.accounts.size.should == 10
119
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2014-05-01 00:00:00"]
120
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] }.sort.should == ["identity_client", "vc-promove"]
121
+ end
122
+ it "should find the requested profile by uuid, including other services and expired accounts" do
123
+ identity = described_class.find("5e32f927-c4ab-404e-a91c-b2abc05afb56", true, true)
124
+ identity.accounts.size.should == 11
125
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2013-04-02 00:00:00", "2014-05-01 00:00:00"]
126
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] }.sort.should == ["identity_client", "vc-promove"]
127
+ end
128
+ it "should raise an error if no profiles exist with that uuid" do
129
+ expect {
130
+ described_class.find("invalid-uuid")
131
+ }.to raise_error(RestClient::ResourceNotFound, '404 Resource Not Found')
132
+ end
133
+ end
134
+
135
+ describe ".find_by_email", :vcr => true do
136
+ it "should find the requested profile by email" do
137
+ identity = described_class.find_by_email("teste@teste.com")
138
+ identity.should be_instance_of(described_class)
139
+ identity.uuid.should == '5e32f927-c4ab-404e-a91c-b2abc05afb56'
140
+ identity.should be_persisted
141
+ identity.email.should == 'teste@teste.com'
142
+ identity.update_info_url.should == '/accounts/api/identities/5e32f927-c4ab-404e-a91c-b2abc05afb56/'
143
+ identity.accounts.size.should == 9
144
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2014-05-01 00:00:00"]
145
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] rescue nil }.sort.should == [nil]
146
+ identity.services.size.should == 1
147
+ identity.services.keys.should == ['identity_client']
148
+ end
149
+ it "should find the requested profile by email, including expired accounts" do
150
+ identity = described_class.find_by_email("teste@teste.com", true)
151
+ identity.accounts.size.should == 10
152
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2013-04-02 00:00:00", "2014-05-01 00:00:00"]
153
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] rescue nil }.sort.should == [nil]
154
+ identity.services.size.should == 1
155
+ identity.services.keys.should == ['identity_client']
156
+ end
157
+ it "should find the requested profile by email, including other services" do
158
+ identity = described_class.find_by_email("teste@teste.com", false, true)
159
+ identity.accounts.size.should == 10
160
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2014-05-01 00:00:00"]
161
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] }.sort.should == ["identity_client", "vc-promove"]
162
+ end
163
+ it "should find the requested profile by email, including other services and expired accounts" do
164
+ identity = described_class.find_by_email("teste@teste.com", true, true)
165
+ identity.accounts.size.should == 11
166
+ identity.accounts.map { |a| a['expiration'] }.uniq.should == [nil, "2013-04-02 00:00:00", "2014-05-01 00:00:00"]
167
+ identity.accounts.map { |a| a['services'] }.flatten.uniq.map { |s| s['slug'] }.sort.should == ["identity_client", "vc-promove"]
168
+ end
169
+ it "should raise an error if no profiles exist with that email" do
170
+ expect {
171
+ described_class.find("invalid@email.com")
172
+ }.to raise_error(RestClient::ResourceNotFound, '404 Resource Not Found')
173
+ end
174
+ end
175
+
176
+ describe ".authenticate", vcr: true do
177
+ it "should return an instance of Identity if the password is correct for the given email" do
178
+ identity = described_class.authenticate('teste@teste.com', '123456')
179
+ identity.should be_instance_of(described_class)
180
+ identity.should be_persisted
181
+ identity.uuid.should == '5e32f927-c4ab-404e-a91c-b2abc05afb56'
182
+ identity.email.should == 'teste@teste.com'
183
+ identity.update_info_url.should == '/accounts/api/identities/5e32f927-c4ab-404e-a91c-b2abc05afb56/'
184
+ end
185
+ it "should return false if the password is wrong for the given email" do
186
+ described_class.authenticate('teste@teste.com', 'wrong password').should be_false
187
+ end
188
+ it "should return false if no Identity exists on PassaporteWeb with that email" do
189
+ described_class.authenticate('non_existing_email@teste.com', 'some password')
190
+ end
191
+ end
192
+
193
+ describe "#authenticate", vcr: true do
194
+ let(:identity) { described_class.find("5e32f927-c4ab-404e-a91c-b2abc05afb56") }
195
+ it "should return true if the password is correct" do
196
+ identity.authenticate('123456').should be_true
197
+ end
198
+ it "should return false if the password is wrong" do
199
+ identity.authenticate('wrong password').should be_false
200
+ end
201
+ it "should raise an error if the email is not set" do
202
+ identity.instance_variable_set(:@email, nil)
203
+ expect {
204
+ identity.authenticate('some password')
205
+ }.to raise_error(ArgumentError, 'email must be set')
206
+ end
207
+ end
208
+
209
+ describe "#save", :vcr => true do
210
+ describe "PUT" do
211
+ let(:identity) { described_class.find("5e32f927-c4ab-404e-a91c-b2abc05afb56") }
212
+ context "on success" do
213
+ it "should update the profile attributes on the server" do
214
+ identity.first_name.should == 'Testador'
215
+ identity.should be_persisted
216
+ identity.first_name = 'Testador 2'
217
+ identity.save.should be_true
218
+ identity.should be_persisted
219
+ identity.first_name.should == 'Testador 2'
220
+
221
+ identity = described_class.find("5e32f927-c4ab-404e-a91c-b2abc05afb56")
222
+ identity.first_name.should == 'Testador 2'
223
+ end
224
+ end
225
+ context "on failure" do
226
+ it "should return false and set the errors hash" do
227
+ identity.cpf = 42
228
+ identity.should be_persisted
229
+ identity.save.should be_false
230
+ identity.should be_persisted
231
+ identity.errors.should == {"cpf" => ["Certifique-se de que o valor tenha no mínimo 11 caracteres (ele possui 2)."]}
232
+ end
233
+ end
234
+ end
235
+ describe "POST" do
236
+ context "on success" do
237
+ it "should save with password, password2 and must_change_password" do
238
+ attributes = {
239
+ "email" => "lula_luis2002@example.com",
240
+ "first_name" => "Luis Inácio",
241
+ "last_name" => "da Silva",
242
+ "password" => "rW5oHxYB",
243
+ "password2" => "rW5oHxYB",
244
+ "must_change_password" => true,
245
+ "tos" => true
246
+ }
247
+ identity = described_class.new(attributes)
248
+ identity.should_not be_persisted
249
+ identity.save.should be_true
250
+ identity.should be_persisted
251
+ end
252
+ it "should save with all params" do
253
+ attributes = {
254
+ "email" => "lula_luis2006@example.com",
255
+ "first_name" => "Luis Inácio",
256
+ "last_name" => "da Silva",
257
+ "password" => "rW5oHxYB",
258
+ "password2" => "rW5oHxYB",
259
+ "must_change_password" => true,
260
+ "tos" => true,
261
+ "inhibit_activation_message" => false,
262
+ "cpf" => "613.862.250-29",
263
+ "send_partner_news" => false,
264
+ "send_myfreecomm_news" => false
265
+ }
266
+ identity = described_class.new(attributes)
267
+ identity.should_not be_persisted
268
+ identity.save.should be_true
269
+ identity.should be_persisted
270
+ end
271
+ end
272
+ context "on failure" do
273
+ it "should not save" do
274
+ attributes = {
275
+ "email" => "lula_luis81@example.com",
276
+ "first_name" => "Luis Inácio",
277
+ "last_name" => "da Silva",
278
+ "tos" => true
279
+ }
280
+ identity = described_class.new(attributes)
281
+ identity.should_not be_persisted
282
+ identity.save.should_not be_true
283
+ identity.should_not be_persisted
284
+ identity.errors.should == {"password2"=>["Este campo é obrigatório."], "password"=>["Este campo é obrigatório."]}
285
+ end
286
+ end
287
+ end
288
+ end
289
+
290
+ end
@@ -0,0 +1,150 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe PassaporteWeb::ServiceAccountMember do
5
+ let(:service_account) { PassaporteWeb::ServiceAccount.find('859d3542-84d6-4909-b1bd-4f43c1312065') }
6
+ let(:identity) { PassaporteWeb::Identity.find('5e32f927-c4ab-404e-a91c-b2abc05afb56') }
7
+
8
+ let(:mock_service_account) { mock('ServiceAccount', uuid: 'service-account-uuid') }
9
+ let(:mock_identity) { mock('Identity', uuid: 'identity-uuid') }
10
+
11
+ describe ".new" do
12
+ it "should instanciate a minumum object" do
13
+ member = described_class.new(mock_service_account, mock_identity)
14
+ member.service_account.should == mock_service_account
15
+ member.identity.should == mock_identity
16
+ member.roles.should == ['user']
17
+ member.membership_details_url.should be_nil
18
+ member.errors.should be_empty
19
+ member.should_not be_persisted
20
+ member.should_not be_destroyed
21
+ end
22
+ it "should instanciate an object with attributes set" do
23
+ member = described_class.new(mock_service_account, mock_identity, ['admin', 'user'])
24
+ member.roles.should == ['admin', 'user']
25
+ end
26
+ end
27
+
28
+ describe ".find", vcr: true do
29
+ it "should return an instance of ServiceAccountMember with all attributes set" do
30
+ member = described_class.find(service_account, identity)
31
+ member.service_account.should == service_account
32
+ member.identity.should == identity
33
+ member.roles.should == ['admin','user']
34
+ member.membership_details_url.should == "/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/5e32f927-c4ab-404e-a91c-b2abc05afb56/"
35
+ member.errors.should be_empty
36
+ member.should be_persisted
37
+ member.should_not be_destroyed
38
+ end
39
+ it "should raise an 404 error if the membership does not exist" do
40
+ expect {
41
+ described_class.find(service_account, mock('Identity', uuid: 'identity-uuid'))
42
+ }.to raise_error(RestClient::ResourceNotFound, '404 Resource Not Found')
43
+ end
44
+ end
45
+
46
+ describe "#destroy", vcr: true do
47
+ it "should destroy the membership, removing the association between service_account and identity" do
48
+ member = described_class.find(service_account, identity)
49
+ member.destroy.should be_true
50
+ member.should_not be_persisted
51
+ member.should be_destroyed
52
+ member.errors.should be_empty
53
+ expect {
54
+ described_class.find(service_account, identity)
55
+ }.to raise_error(RestClient::ResourceNotFound, '404 Resource Not Found')
56
+ end
57
+ it "should return false if the role is owner" do
58
+ member = described_class.find(service_account, identity)
59
+ member.roles.should include('owner')
60
+ member.destroy.should be_false
61
+ member.should be_persisted
62
+ member.should_not be_destroyed
63
+ member.errors.should == "Service owner cannot be removed from members list"
64
+
65
+ expect {
66
+ described_class.find(service_account, identity)
67
+ }.not_to raise_error
68
+ end
69
+ end
70
+
71
+ describe "#save", vcr: true do
72
+ context "when creating" do
73
+ let(:member) { described_class.new(service_account, identity, ['admin','user']) }
74
+ context "on success" do
75
+ it "should create the membership between the service_account and the identity" do
76
+ member.save.should be_true
77
+ member.service_account.should == service_account
78
+ member.identity.should == identity
79
+ member.roles.should == ['admin','user']
80
+ member.membership_details_url.should == "/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/5e32f927-c4ab-404e-a91c-b2abc05afb56/"
81
+ member.errors.should be_empty
82
+ member.should be_persisted
83
+ member.should_not be_destroyed
84
+
85
+ member = described_class.find(service_account, identity)
86
+ member.service_account.should == service_account
87
+ member.identity.should == identity
88
+ member.roles.should == ['admin','user']
89
+ member.membership_details_url.should == "/organizations/api/accounts/859d3542-84d6-4909-b1bd-4f43c1312065/members/5e32f927-c4ab-404e-a91c-b2abc05afb56/"
90
+ member.errors.should be_empty
91
+ member.should be_persisted
92
+ member.should_not be_destroyed
93
+ end
94
+ end
95
+ context "on failure" do
96
+ it "should not create the membership and set the errors on the object" do
97
+ member.roles = ['owner'] # can't create membership for the owner
98
+ member.save.should be_false
99
+ member.errors.should == "Adding a member as owner is not allowed"
100
+ member.should_not be_persisted
101
+ member.should_not be_destroyed
102
+
103
+ expect {
104
+ described_class.find(service_account, identity)
105
+ }.to raise_error(RestClient::ResourceNotFound, '404 Resource Not Found')
106
+ end
107
+ it "should return false if the membership already exists" do
108
+ member.save.should be_false
109
+ member.errors.should == "Identity with uuid=5e32f927-c4ab-404e-a91c-b2abc05afb56 is already in members list of service identity_client at account 859d3542-84d6-4909-b1bd-4f43c1312065"
110
+ member.should_not be_persisted
111
+ member.should_not be_destroyed
112
+
113
+ expect {
114
+ described_class.find(service_account, identity)
115
+ }.not_to raise_error
116
+ end
117
+ end
118
+ end
119
+ context "when updating" do
120
+ let(:member) { described_class.find(service_account, identity) }
121
+ context "on success" do
122
+ it "should update the member roles" do
123
+ member.roles.should == ['admin', 'user']
124
+ member.roles = ['user']
125
+ member.save.should be_true
126
+ member.errors.should be_empty
127
+ member.roles.should == ['user']
128
+
129
+ member = described_class.find(service_account, identity)
130
+ member.roles.should == ['user']
131
+ end
132
+ end
133
+ context "on failure" do
134
+ it "should return false and set the errors" do
135
+ pending "está deixando setar como owner e não deixa setar sem nenhum role, como fazer?"
136
+ member.roles.should == ['admin', 'user']
137
+ member.roles = ['owner']
138
+ member.save.should be_false
139
+ binding.pry
140
+ member.errors.should_not be_empty
141
+ member.roles.should == ['owner']
142
+
143
+ member = described_class.find(service_account, identity)
144
+ member.roles.should == ['admin', 'user']
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ end