openstax_accounts 7.9.0 → 7.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/handlers/openstax/accounts/sessions_callback.rb +1 -0
- data/app/models/openstax/accounts/account.rb +3 -3
- data/app/representers/openstax/accounts/api/v1/account_representer.rb +6 -0
- data/app/representers/openstax/accounts/api/v1/unclaimed_account_representer.rb +6 -0
- data/app/routines/openstax/accounts/dev/create_account.rb +2 -1
- data/app/routines/openstax/accounts/find_or_create_account.rb +3 -0
- data/app/routines/openstax/accounts/sync_accounts.rb +9 -6
- data/db/migrate/11_add_support_identifier_to_accounts_accounts.rb +8 -0
- data/lib/openstax/accounts/version.rb +1 -1
- data/spec/cassettes/OpenStax_Accounts_FindOrCreateAccount/can_create_users.yml +9 -9
- data/spec/controllers/openstax/accounts/dev/accounts_controller_spec.rb +1 -1
- data/spec/controllers/openstax/accounts/forwards_params_spec.rb +1 -1
- data/spec/controllers/openstax/accounts/sessions_controller_spec.rb +1 -1
- data/spec/controllers/openstax/accounts/uses_this_engine_controller_spec.rb +1 -1
- data/spec/dummy/db/schema.rb +3 -0
- data/spec/dummy/log/development.log +560 -0
- data/spec/dummy/log/test.log +27696 -0
- data/spec/factories/openstax_accounts_account.rb +7 -6
- data/spec/handlers/openstax/accounts/accounts_search_spec.rb +1 -1
- data/spec/handlers/openstax/accounts/dev/accounts_search_spec.rb +1 -1
- data/spec/handlers/openstax/accounts/sessions_callback_spec.rb +27 -7
- data/spec/lib/openstax/accounts/api_spec.rb +1 -1
- data/spec/lib/openstax/accounts/configuration_spec.rb +1 -1
- data/spec/lib/openstax/accounts/current_user_manager_spec.rb +1 -1
- data/spec/lib/openstax/accounts/has_many_through_groups/active_record/base_spec.rb +1 -1
- data/spec/models/openstax/accounts/account_spec.rb +5 -1
- data/spec/models/openstax/accounts/anonymous_account_spec.rb +1 -1
- data/spec/models/openstax/accounts/group_spec.rb +1 -1
- data/spec/routines/openstax/accounts/create_group_spec.rb +1 -1
- data/spec/routines/openstax/accounts/find_or_create_account_spec.rb +18 -8
- data/spec/routines/openstax/accounts/search_accounts_spec.rb +1 -1
- data/spec/routines/openstax/accounts/sync_accounts_spec.rb +39 -29
- data/spec/routines/openstax/accounts/sync_groups_spec.rb +47 -65
- data/spec/spec_helper.rb +17 -9
- metadata +17 -2
@@ -1,10 +1,11 @@
|
|
1
1
|
FactoryBot.define do
|
2
2
|
factory :openstax_accounts_account, class: OpenStax::Accounts::Account do
|
3
|
-
openstax_uid
|
4
|
-
username
|
5
|
-
access_token
|
6
|
-
faculty_status
|
7
|
-
role
|
8
|
-
uuid
|
3
|
+
openstax_uid { -SecureRandom.hex(4).to_i(16)/2 }
|
4
|
+
username { SecureRandom.hex.to_s }
|
5
|
+
access_token { SecureRandom.hex.to_s }
|
6
|
+
faculty_status { OpenStax::Accounts::Account.faculty_statuses[:no_faculty_info] }
|
7
|
+
role { OpenStax::Accounts::Account.roles[:unknown_role] }
|
8
|
+
uuid { SecureRandom.uuid }
|
9
|
+
support_identifier { "cs_#{SecureRandom.hex(4)}" }
|
9
10
|
end
|
10
11
|
end
|
@@ -3,22 +3,25 @@ require 'spec_helper'
|
|
3
3
|
module OpenStax
|
4
4
|
module Accounts
|
5
5
|
|
6
|
-
describe SessionsCallback do
|
6
|
+
RSpec.describe SessionsCallback do
|
7
7
|
|
8
8
|
context "faculty_status" do
|
9
|
-
it "should
|
10
|
-
|
9
|
+
it "should default to no_faculty_info if the received faculty_status is unknown" +
|
10
|
+
" (e.g. if Accounts is updated but this repo is not)" do
|
11
|
+
result = described_class.handle(
|
12
|
+
request: mock_omniauth_request(faculty_status: "howdy_ho")
|
13
|
+
)
|
11
14
|
expect(result.outputs.account).to be_no_faculty_info
|
12
15
|
end
|
13
16
|
|
14
|
-
it "should
|
17
|
+
it "should default to no_faculty_info if faculty status is not present" do
|
15
18
|
request = mock_omniauth_request()
|
16
19
|
remove_faculty_status!(request)
|
17
20
|
result = described_class.handle(request: request)
|
18
21
|
expect(result.outputs.account).to be_no_faculty_info
|
19
22
|
end
|
20
23
|
|
21
|
-
it "should deal with null
|
24
|
+
it "should deal with null nicknames" do
|
22
25
|
with_stubbing(false) do
|
23
26
|
request = mock_omniauth_request
|
24
27
|
remove_nickname!(request)
|
@@ -37,14 +40,28 @@ module OpenStax
|
|
37
40
|
end
|
38
41
|
end
|
39
42
|
|
43
|
+
context "support_identifier" do
|
44
|
+
it "sets the support_identifier on the account" do
|
45
|
+
support_identifier = "cs_#{SecureRandom.hex(4)}"
|
46
|
+
result = described_class.handle(
|
47
|
+
request: mock_omniauth_request(support_identifier: support_identifier)
|
48
|
+
)
|
49
|
+
expect(result.outputs.account.support_identifier).to eq support_identifier
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
40
53
|
context "role" do
|
41
54
|
it "sets the role on the account" do
|
42
|
-
result = described_class.handle(
|
55
|
+
result = described_class.handle(
|
56
|
+
request: mock_omniauth_request(self_reported_role: "instructor")
|
57
|
+
)
|
43
58
|
expect(result.outputs.account.role).to eq "instructor"
|
44
59
|
end
|
45
60
|
|
46
61
|
it "deals with unknown role (e.g. if Accounts update but this repo not)" do
|
47
|
-
result = described_class.handle(
|
62
|
+
result = described_class.handle(
|
63
|
+
request: mock_omniauth_request(self_reported_role: "howdy_ho")
|
64
|
+
)
|
48
65
|
expect(result.outputs.account).to be_unknown_role
|
49
66
|
end
|
50
67
|
end
|
@@ -53,6 +70,7 @@ module OpenStax
|
|
53
70
|
it "updates the user's data" do
|
54
71
|
existing_account = FactoryBot.create :openstax_accounts_account
|
55
72
|
uuid = SecureRandom.uuid
|
73
|
+
support_identifier = "cs_#{SecureRandom.hex(4)}"
|
56
74
|
result = described_class.handle(
|
57
75
|
request: mock_omniauth_request(
|
58
76
|
uid: existing_account.openstax_uid,
|
@@ -62,6 +80,7 @@ module OpenStax
|
|
62
80
|
nickname: "191919",
|
63
81
|
faculty_status: "confirmed_faculty",
|
64
82
|
uuid: uuid,
|
83
|
+
support_identifier: support_identifier,
|
65
84
|
self_reported_role: "instructor")
|
66
85
|
)
|
67
86
|
|
@@ -73,6 +92,7 @@ module OpenStax
|
|
73
92
|
expect(account.username).to eq "191919"
|
74
93
|
expect(account).to be_confirmed_faculty
|
75
94
|
expect(account.uuid).to eq uuid
|
95
|
+
expect(account.support_identifier).to eq support_identifier
|
76
96
|
expect(account).to be_instructor
|
77
97
|
end
|
78
98
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module OpenStax
|
2
2
|
module Accounts
|
3
|
-
describe Configuration do
|
3
|
+
RSpec.describe Configuration do
|
4
4
|
|
5
5
|
let!(:config) { Configuration.new.tap {|c| c.openstax_accounts_url = "https://accounts.openstax.org"} }
|
6
6
|
let!(:a_fake_request) { OpenStruct.new(url: "http://foo.com") }
|
@@ -1,9 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module OpenStax::Accounts
|
4
|
-
RSpec.describe Account do
|
4
|
+
RSpec.describe Account, type: :model do
|
5
5
|
subject(:account) { FactoryBot.create(:openstax_accounts_account) }
|
6
6
|
|
7
|
+
it { is_expected.to validate_presence_of(:uuid) }
|
8
|
+
it { is_expected.to validate_uniqueness_of(:uuid).case_insensitive }
|
9
|
+
it { is_expected.to validate_uniqueness_of(:support_identifier).case_insensitive.allow_nil }
|
10
|
+
|
7
11
|
context 'validation' do
|
8
12
|
it 'requires a unique openstax_uid, if given' do
|
9
13
|
account.openstax_uid = nil
|
@@ -4,7 +4,7 @@ require 'vcr_helper'
|
|
4
4
|
module OpenStax
|
5
5
|
module Accounts
|
6
6
|
|
7
|
-
describe FindOrCreateAccount, type: :routine, vcr: VCR_OPTS do
|
7
|
+
RSpec.describe FindOrCreateAccount, type: :routine, vcr: VCR_OPTS do
|
8
8
|
|
9
9
|
before(:all) do
|
10
10
|
@previous_url = OpenStax::Accounts.configuration.openstax_accounts_url
|
@@ -15,17 +15,22 @@ module OpenStax
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'can create users' do
|
18
|
-
account_1 =
|
18
|
+
account_1 = described_class.call(
|
19
|
+
email: 'alice@example.com', role: 'instructor'
|
20
|
+
).outputs.account
|
19
21
|
expect(account_1).to be_persisted
|
20
|
-
expect(account_1.uuid).to
|
22
|
+
expect(account_1.uuid).to(
|
23
|
+
match(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i)
|
24
|
+
)
|
25
|
+
expect(account_1.support_identifier).to match(/\Acs_[0-9a-f]{8}\z/i)
|
21
26
|
expect(account_1.role).to eq 'instructor'
|
22
27
|
|
23
|
-
account_2 =
|
28
|
+
account_2 = described_class.call(username: 'alice').outputs.account
|
24
29
|
expect(account_2).to be_persisted
|
25
30
|
expect(account_1).not_to eq(account_2)
|
26
31
|
|
27
|
-
account_3 =
|
28
|
-
|
32
|
+
account_3 = described_class.call(username: 'alice2',
|
33
|
+
password: 'abcdefghijklmnop').outputs.account
|
29
34
|
expect(account_3).to be_persisted
|
30
35
|
expect(account_1).not_to eq(account_3)
|
31
36
|
expect(account_2).not_to eq(account_3)
|
@@ -34,7 +39,12 @@ module OpenStax
|
|
34
39
|
it 'passes params to the API when creating users' do
|
35
40
|
find_or_create_account_response = double('Response')
|
36
41
|
allow(find_or_create_account_response).to receive(:status).and_return(201)
|
37
|
-
allow(find_or_create_account_response).to
|
42
|
+
allow(find_or_create_account_response).to(
|
43
|
+
receive(:body).and_return(
|
44
|
+
"{\"id\":1,\"uuid\":\"#{SecureRandom.uuid
|
45
|
+
}\",\"support_identifier\":\"cs_#{SecureRandom.hex(4)}\"}"
|
46
|
+
)
|
47
|
+
)
|
38
48
|
expect(OpenStax::Accounts::Api).to receive(:find_or_create_account).with(
|
39
49
|
email: 'bob@example.com', username: nil, password: nil,
|
40
50
|
first_name: 'Bob', last_name: 'Smith', full_name: 'Bob Smith',
|
@@ -42,7 +52,7 @@ module OpenStax
|
|
42
52
|
role: :instructor
|
43
53
|
).and_return(find_or_create_account_response)
|
44
54
|
|
45
|
-
|
55
|
+
described_class.call(
|
46
56
|
email: 'bob@example.com',
|
47
57
|
first_name: 'Bob',
|
48
58
|
last_name: 'Smith',
|
@@ -3,11 +3,14 @@ require 'spec_helper'
|
|
3
3
|
module OpenStax
|
4
4
|
module Accounts
|
5
5
|
|
6
|
-
describe SyncAccounts, type: :routine do
|
6
|
+
RSpec.describe SyncAccounts, type: :routine do
|
7
7
|
|
8
8
|
it 'can sync accounts' do
|
9
9
|
controller_class = ::Api::ApplicationUsersController
|
10
|
-
|
10
|
+
uuid_1 = SecureRandom.uuid
|
11
|
+
uuid_2 = SecureRandom.uuid
|
12
|
+
support_identifier_1 = "cs_#{SecureRandom.hex(4)}"
|
13
|
+
support_identifier_2 = "cs_#{SecureRandom.hex(4)}"
|
11
14
|
allow_any_instance_of(controller_class).to(
|
12
15
|
receive(:updates) do |controller|
|
13
16
|
controller.render json: [
|
@@ -17,8 +20,9 @@ module OpenStax
|
|
17
20
|
user: {
|
18
21
|
id: 2,
|
19
22
|
username: 'user',
|
20
|
-
|
21
|
-
|
23
|
+
uuid: uuid_1,
|
24
|
+
support_identifier: support_identifier_1,
|
25
|
+
self_reported_role: 'instructor'
|
22
26
|
},
|
23
27
|
unread_updates: 1,
|
24
28
|
default_contact_info_id: 1
|
@@ -28,6 +32,8 @@ module OpenStax
|
|
28
32
|
application_id: 1,
|
29
33
|
user: {
|
30
34
|
id: 4,
|
35
|
+
uuid: uuid_2,
|
36
|
+
support_identifier: support_identifier_2,
|
31
37
|
username: 'fuego'
|
32
38
|
},
|
33
39
|
unread_updates: 2,
|
@@ -37,46 +43,50 @@ module OpenStax
|
|
37
43
|
end
|
38
44
|
)
|
39
45
|
|
40
|
-
account =
|
41
|
-
account.username = 'u'
|
42
|
-
account.openstax_uid = 2
|
46
|
+
account = FactoryBot.create :openstax_accounts_account, username: 'u', openstax_uid: 2
|
43
47
|
account.syncing = true
|
44
|
-
account.save!
|
45
48
|
|
46
49
|
begin
|
47
50
|
OpenStax::Accounts.configuration.enable_stubbing = false
|
48
|
-
expect(
|
49
|
-
expect(
|
50
|
-
expect(Account.
|
51
|
+
expect(account.reload.openstax_uid).to eq 2
|
52
|
+
expect(account.username).to eq 'u'
|
53
|
+
expect(Account.find_by(openstax_uid: 4)).to be_nil
|
51
54
|
|
52
55
|
controller_class.last_action = nil
|
53
56
|
controller_class.last_json = nil
|
54
|
-
|
55
|
-
expect
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
expect(
|
60
|
-
expect(
|
61
|
-
expect(
|
57
|
+
|
58
|
+
expect { SyncAccounts.call }.to change { Account.count }.by(1)
|
59
|
+
|
60
|
+
account2 = Account.find_by(openstax_uid: 4)
|
61
|
+
|
62
|
+
expect(account.reload.openstax_uid).to eq 2
|
63
|
+
expect(account.username).to eq 'user'
|
64
|
+
expect(account.role).to eq 'instructor'
|
65
|
+
expect(account.uuid).to eq uuid_1
|
66
|
+
expect(account.support_identifier).to eq support_identifier_1
|
67
|
+
expect(account2.username).to eq 'fuego'
|
68
|
+
expect(account2.uuid).to eq uuid_2
|
69
|
+
expect(account2.support_identifier).to eq support_identifier_2
|
62
70
|
|
63
71
|
expect(controller_class.last_action).to eq :updated
|
64
|
-
expect(controller_class.last_json).to eq [
|
65
|
-
|
72
|
+
expect(controller_class.last_json).to eq [
|
73
|
+
{'user_id' => 2, 'read_updates' => 1}, {'user_id' => 4, 'read_updates' => 2}
|
74
|
+
]
|
66
75
|
|
67
76
|
controller_class.last_action = nil
|
68
77
|
controller_class.last_json = nil
|
69
78
|
|
70
|
-
SyncAccounts.call
|
71
|
-
|
72
|
-
expect(
|
73
|
-
expect(
|
74
|
-
expect(
|
75
|
-
expect(
|
79
|
+
expect { SyncAccounts.call }.not_to change { Account.count }
|
80
|
+
|
81
|
+
expect(account.reload.openstax_uid).to eq 2
|
82
|
+
expect(account.username).to eq 'user'
|
83
|
+
expect(account2.reload.openstax_uid).to eq 4
|
84
|
+
expect(account2.username).to eq 'fuego'
|
76
85
|
|
77
86
|
expect(controller_class.last_action).to eq :updated
|
78
|
-
expect(controller_class.last_json).to eq [
|
79
|
-
|
87
|
+
expect(controller_class.last_json).to eq [
|
88
|
+
{'user_id' => 2, 'read_updates' => 1}, {'user_id' => 4, 'read_updates' => 2}
|
89
|
+
]
|
80
90
|
ensure
|
81
91
|
OpenStax::Accounts.configuration.enable_stubbing = true
|
82
92
|
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module OpenStax
|
4
4
|
module Accounts
|
5
5
|
|
6
|
-
describe SyncGroups, type: :routine do
|
6
|
+
RSpec.describe SyncGroups, type: :routine do
|
7
7
|
|
8
8
|
it 'can sync groups' do
|
9
9
|
controller_class = ::Api::ApplicationGroupsController
|
@@ -61,95 +61,77 @@ module OpenStax
|
|
61
61
|
end
|
62
62
|
)
|
63
63
|
|
64
|
-
u =
|
65
|
-
u.openstax_uid = 2
|
66
|
-
u.username = 'User'
|
64
|
+
u = FactoryBot.create :openstax_accounts_account, openstax_uid: 2, username: 'User'
|
67
65
|
u.syncing = true
|
68
|
-
u.save!
|
69
66
|
|
70
|
-
u2 =
|
71
|
-
u2.openstax_uid = 3
|
72
|
-
u2.username = 'Fuego'
|
67
|
+
u2 = FactoryBot.create :openstax_accounts_account, openstax_uid: 3, username: 'Fuego'
|
73
68
|
u2.syncing = true
|
74
|
-
u2.save!
|
75
69
|
|
76
|
-
g =
|
77
|
-
g.name = 'Member Group'
|
78
|
-
g.openstax_uid = 2
|
70
|
+
g = FactoryBot.create :openstax_accounts_group, openstax_uid: 2, name: 'Member Group'
|
79
71
|
g.syncing = true
|
80
|
-
g.save!
|
81
72
|
|
82
|
-
gm =
|
83
|
-
gm.group = g
|
84
|
-
gm.user = u
|
85
|
-
gm.save!
|
73
|
+
gm = FactoryBot.create :openstax_accounts_group_member, group: g, user: u
|
86
74
|
|
87
|
-
g2 =
|
88
|
-
g2.name = 'Container Group'
|
89
|
-
g2.openstax_uid = 4
|
75
|
+
g2 = FactoryBot.create :openstax_accounts_group, openstax_uid: 4, name: 'Container Group'
|
90
76
|
g2.syncing = true
|
91
|
-
g2.save!
|
92
77
|
|
93
|
-
go =
|
94
|
-
go.group = g2
|
95
|
-
go.user = u
|
96
|
-
go.save!
|
78
|
+
go = FactoryBot.create :openstax_accounts_group_owner, group: g2, user: u
|
97
79
|
|
98
|
-
gn =
|
99
|
-
|
100
|
-
gn.member_group = g
|
101
|
-
gn.save!
|
80
|
+
gn = FactoryBot.create :openstax_accounts_group_nesting, container_group: g2,
|
81
|
+
member_group: g
|
102
82
|
|
103
83
|
begin
|
104
84
|
OpenStax::Accounts.configuration.enable_stubbing = false
|
105
|
-
expect(
|
106
|
-
expect(
|
107
|
-
expect(
|
108
|
-
expect(
|
109
|
-
expect(
|
110
|
-
expect(
|
111
|
-
expect(
|
112
|
-
expect(Group.last.owners).to include u
|
85
|
+
expect(g.reload.openstax_uid).to eq 2
|
86
|
+
expect(g.name).to eq 'Member Group'
|
87
|
+
expect(g.members).to eq [ u ]
|
88
|
+
expect(g2.reload.openstax_uid).to eq 4
|
89
|
+
expect(g2.name).to eq 'Container Group'
|
90
|
+
expect(g2.member_groups).to eq [ g ]
|
91
|
+
expect(g2.owners).to eq [ u ]
|
113
92
|
|
114
93
|
controller_class.last_action = nil
|
115
94
|
controller_class.last_json = nil
|
116
95
|
|
117
|
-
SyncGroups.call
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
expect(
|
122
|
-
expect(
|
123
|
-
expect(
|
124
|
-
expect(
|
125
|
-
expect(
|
126
|
-
expect(
|
127
|
-
expect(
|
128
|
-
expect(
|
96
|
+
expect { SyncGroups.call }.to change { Group.count }.by(1)
|
97
|
+
|
98
|
+
g3 = Group.find_by(openstax_uid: 3)
|
99
|
+
|
100
|
+
expect(g.reload.openstax_uid).to eq 2
|
101
|
+
expect(g.name).to eq 'M'
|
102
|
+
expect(g.member_groups).to eq [ g3 ]
|
103
|
+
expect(g2.reload.openstax_uid).to eq 4
|
104
|
+
expect(g2.name).to eq 'Container Group'
|
105
|
+
expect(g2.member_groups).to eq [ g ]
|
106
|
+
expect(g3.name).to eq 'Fuego\'s Deputies'
|
107
|
+
expect(g3.owners).to eq [ u2 ]
|
108
|
+
expect(g3.members).to eq [ u ]
|
129
109
|
|
130
110
|
expect(controller_class.last_action).to eq :updated
|
131
|
-
expect(controller_class.last_json).to eq [
|
132
|
-
|
111
|
+
expect(controller_class.last_json).to eq [
|
112
|
+
{'group_id' => 2, 'read_updates' => 1}, {'group_id' => 3, 'read_updates' => 2}
|
113
|
+
]
|
133
114
|
|
134
115
|
controller_class.last_action = nil
|
135
116
|
controller_class.last_json = nil
|
136
117
|
|
137
|
-
SyncGroups.call
|
138
|
-
|
139
|
-
expect(
|
140
|
-
expect(
|
141
|
-
expect(
|
142
|
-
expect(
|
143
|
-
expect(
|
144
|
-
expect(
|
145
|
-
expect(
|
146
|
-
expect(
|
147
|
-
expect(
|
148
|
-
expect(
|
118
|
+
expect { SyncGroups.call }.not_to change { Group.count }
|
119
|
+
|
120
|
+
expect(g.reload.openstax_uid).to eq 2
|
121
|
+
expect(g.name).to eq 'M'
|
122
|
+
expect(g.member_groups).to eq [ g3 ]
|
123
|
+
expect(g2.reload.openstax_uid).to eq 4
|
124
|
+
expect(g2.name).to eq 'Container Group'
|
125
|
+
expect(g2.member_groups).to eq [ g ]
|
126
|
+
expect(g3.reload.openstax_uid).to eq 3
|
127
|
+
expect(g3.name).to eq 'Fuego\'s Deputies'
|
128
|
+
expect(g3.owners).to eq [ u2 ]
|
129
|
+
expect(g3.members).to eq [ u ]
|
149
130
|
|
150
131
|
expect(controller_class.last_action).to eq :updated
|
151
|
-
expect(controller_class.last_json).to eq [
|
152
|
-
|
132
|
+
expect(controller_class.last_json).to eq [
|
133
|
+
{'group_id' => 2, 'read_updates' => 1}, {'group_id' => 3, 'read_updates' => 2}
|
134
|
+
]
|
153
135
|
ensure
|
154
136
|
OpenStax::Accounts.configuration.enable_stubbing = true
|
155
137
|
end
|