openstax_accounts 4.1.1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -1
  3. data/app/controllers/openstax/accounts/application_controller.rb +1 -1
  4. data/app/models/openstax/accounts/account.rb +11 -6
  5. data/app/models/openstax/accounts/anonymous_account.rb +1 -1
  6. data/app/models/openstax/accounts/application_account.rb +2 -1
  7. data/app/models/openstax/accounts/group.rb +14 -12
  8. data/app/models/openstax/accounts/group_member.rb +10 -10
  9. data/app/models/openstax/accounts/group_nesting.rb +7 -13
  10. data/app/models/openstax/accounts/group_owner.rb +10 -10
  11. data/app/representers/openstax/accounts/api/v1/account_representer.rb +4 -3
  12. data/app/representers/openstax/accounts/api/v1/application_account_representer.rb +7 -2
  13. data/app/representers/openstax/accounts/api/v1/application_accounts_representer.rb +0 -2
  14. data/app/representers/openstax/accounts/api/v1/application_group_representer.rb +7 -2
  15. data/app/representers/openstax/accounts/api/v1/application_groups_representer.rb +0 -2
  16. data/app/representers/openstax/accounts/api/v1/group_nesting_representer.rb +1 -1
  17. data/app/representers/openstax/accounts/api/v1/group_representer.rb +1 -1
  18. data/app/representers/openstax/accounts/api/v1/group_user_representer.rb +1 -1
  19. data/app/routines/openstax/accounts/search_accounts.rb +1 -1
  20. data/app/routines/openstax/accounts/sync_accounts.rb +24 -27
  21. data/app/routines/openstax/accounts/sync_groups.rb +22 -26
  22. data/lib/openstax/accounts/action_controller/base.rb +61 -0
  23. data/lib/openstax/accounts/api.rb +270 -0
  24. data/lib/openstax/accounts/configuration.rb +77 -0
  25. data/lib/openstax/accounts/engine.rb +13 -11
  26. data/lib/openstax/accounts/has_many_through_groups/active_record/base.rb +51 -0
  27. data/lib/openstax/accounts/version.rb +1 -1
  28. data/lib/openstax_accounts.rb +8 -331
  29. data/spec/dummy/app/controllers/api/users_controller.rb +4 -0
  30. data/spec/dummy/config/routes.rb +3 -1
  31. data/spec/dummy/log/test.log +152307 -0
  32. data/spec/lib/openstax/accounts/api_spec.rb +232 -0
  33. data/spec/lib/openstax/accounts/has_many_through_groups/active_record/base_spec.rb +57 -0
  34. data/spec/routines/openstax/accounts/sync_accounts_spec.rb +5 -9
  35. data/spec/routines/openstax/accounts/sync_groups_spec.rb +38 -39
  36. metadata +27 -16
  37. data/lib/openstax/accounts/extend_builtins.rb +0 -50
  38. data/lib/openstax/accounts/has_many_through_groups.rb +0 -47
  39. data/spec/lib/openstax/accounts/has_many_through_groups_spec.rb +0 -53
  40. data/spec/lib/openstax_accounts_spec.rb +0 -210
@@ -1,50 +0,0 @@
1
- ActionController::Base.class_exec do
2
-
3
- helper_method :current_user, :signed_in?
4
-
5
- # Returns the current user
6
- def current_user
7
- current_user_manager.current_user
8
- end
9
-
10
- # Returns the current account
11
- def current_account
12
- current_user_manager.current_account
13
- end
14
-
15
- # Returns true iff there is a user signed in
16
- def signed_in?
17
- current_user_manager.signed_in?
18
- end
19
-
20
- # Signs in the given account or user
21
- def sign_in(user)
22
- current_user_manager.sign_in(user)
23
- end
24
-
25
- # Signs out the current account and user
26
- def sign_out!
27
- current_user_manager.sign_out!
28
- end
29
-
30
- protected
31
-
32
- def current_user_manager
33
- @current_user_manager ||= OpenStax::Accounts::CurrentUserManager.new(
34
- request, session, cookies)
35
- end
36
-
37
- def authenticate_user!
38
- account = current_account
39
-
40
- return if account && !account.is_anonymous?
41
-
42
- store_url key: :accounts_return_to, strategies: [:session]
43
-
44
- respond_to do |format|
45
- format.html { redirect_to openstax_accounts.login_url }
46
- format.json { head(:forbidden) }
47
- end
48
- end
49
-
50
- end
@@ -1,47 +0,0 @@
1
- module OpenStax
2
- module Accounts
3
- module HasManyThroughGroups
4
- module ActiveRecord
5
- def self.included(base)
6
- base.extend(ClassMethods)
7
- end
8
-
9
- module ClassMethods
10
- def has_many_through_groups(groups_name, name, options = {})
11
- options = {class_name: name.to_s.classify}.merge(options)
12
- association_name = "direct_#{name.to_s}".to_sym
13
-
14
- OpenStax::Accounts::Group.class_exec do
15
- has_many association_name, options
16
-
17
- define_method(name) do
18
- OpenStax::Accounts::Group.includes(association_name)
19
- .where(openstax_uid: supertree_group_ids)
20
- .collect{|g| g.send(association_name).to_a}.flatten.uniq
21
- end
22
- end
23
-
24
- class_exec do
25
- has_many association_name, options if options[:as]
26
-
27
- define_method(name) do
28
- direct_records = respond_to?(association_name) ? \
29
- send(association_name).to_a : []
30
- indirect_records = OpenStax::Accounts::Group
31
- .includes(association_name).where(
32
- openstax_uid: send(groups_name).collect{|g|
33
- g.supertree_group_ids
34
- }.flatten.uniq
35
- )
36
- .collect{|g| g.send(association_name).to_a}
37
- (direct_records + indirect_records).flatten.uniq
38
- end
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
46
-
47
- ActiveRecord::Base.send :include, OpenStax::Accounts::HasManyThroughGroups::ActiveRecord
@@ -1,53 +0,0 @@
1
- module OpenStax
2
- module Accounts
3
- describe HasManyThroughGroups do
4
- let!(:account_1) { FactoryGirl.create(:openstax_accounts_account,
5
- username: 'some_user',
6
- openstax_uid: 1) }
7
- let!(:user_1) { User.create(:account => account_1) }
8
-
9
- let!(:account_2) { FactoryGirl.create(:openstax_accounts_account,
10
- username: 'another_user',
11
- openstax_uid: 2) }
12
- let!(:user_2) { User.create(:account => account_2) }
13
-
14
- let!(:group_nesting) { FactoryGirl.create(:openstax_accounts_group_nesting) }
15
-
16
- before(:each) do
17
- group_nesting.member_group.add_member(account_1)
18
- end
19
-
20
- it 'allows users to retrieve all nested has_many_through_groups objects' do
21
- expect(user_1.ownerships).to be_empty
22
-
23
- o = Ownership.new
24
- o.owner = user_2
25
- o.save!
26
-
27
- expect(user_1.reload.ownerships).to be_empty
28
-
29
- o2 = Ownership.new
30
- o2.owner = user_1
31
- o2.save!
32
-
33
- expect(user_1.reload.ownerships).to include(o2)
34
-
35
- o3 = Ownership.new
36
- o3.owner = group_nesting.member_group
37
- o3.save!
38
-
39
- expect(user_1.reload.ownerships).to include(o2)
40
- expect(user_1.ownerships).to include(o3)
41
-
42
- o4 = Ownership.new
43
- o4.owner = group_nesting.container_group
44
- o4.save!
45
-
46
- expect(user_1.reload.ownerships).to include(o2)
47
- expect(user_1.ownerships).to include(o3)
48
- expect(user_1.ownerships).to include(o4)
49
- end
50
-
51
- end
52
- end
53
- end
@@ -1,210 +0,0 @@
1
- module OpenStax
2
- describe Accounts do
3
-
4
- let!(:account) { OpenStax::Accounts::Account.create(username: 'some_user',
5
- first_name: 'Some', last_name: 'User', full_name: 'SomeUser',
6
- title: 'Sir', openstax_uid: 1, access_token: 'secret') }
7
-
8
- it 'makes api calls' do
9
- expect(::Api::DummyController.last_action).to be_nil
10
- expect(::Api::DummyController.last_params).to be_nil
11
- Accounts.api_call(:post, 'dummy', :params => {:test => true})
12
- expect(::Api::DummyController.last_action).to eq :dummy
13
- expect(::Api::DummyController.last_params).to include 'test' => 'true'
14
- end
15
-
16
- context 'users' do
17
-
18
- let!(:account) { FactoryGirl.create :openstax_accounts_account }
19
-
20
- it 'makes api call to users index' do
21
- ::Api::UsersController.last_action = nil
22
- ::Api::UsersController.last_params = nil
23
- Accounts.search_accounts('something')
24
- expect(::Api::UsersController.last_action).to eq :index
25
- expect(::Api::UsersController.last_params).to include :q => 'something'
26
- end
27
-
28
- it 'makes api call to user update' do
29
- ::Api::UsersController.last_action = nil
30
- ::Api::UsersController.last_json = nil
31
- Accounts.update_account(account)
32
- expect(::Api::UsersController.last_action).to eq :update
33
- expect(::Api::UsersController.last_json).to include(
34
- account.attributes.slice('username', 'first_name',
35
- 'last_name', 'full_name', 'title'))
36
- end
37
-
38
- end
39
-
40
- context 'application_users' do
41
-
42
- it 'makes api call to application_users index' do
43
- ::Api::ApplicationUsersController.last_action = nil
44
- ::Api::ApplicationUsersController.last_params = nil
45
- Accounts.search_application_accounts('something')
46
- expect(::Api::ApplicationUsersController.last_action).to eq :index
47
- expect(::Api::ApplicationUsersController.last_params).to include :q => 'something'
48
- end
49
-
50
- it 'makes api call to application_users updates' do
51
- ::Api::ApplicationUsersController.last_action = nil
52
- Accounts.get_application_account_updates
53
- expect(::Api::ApplicationUsersController.last_action).to eq :updates
54
- end
55
-
56
- it 'makes api call to application_users updated' do
57
- ::Api::ApplicationUsersController.last_action = nil
58
- ::Api::ApplicationUsersController.last_json = nil
59
- Accounts.mark_account_updates_as_read([{id: 1, read_updates: 1}])
60
- expect(::Api::ApplicationUsersController.last_action).to eq :updated
61
- expect(::Api::ApplicationUsersController.last_json).to include(
62
- {'id' => 1, 'read_updates' => 1})
63
- end
64
-
65
- end
66
-
67
- context 'groups' do
68
-
69
- let!(:account) { FactoryGirl.create :openstax_accounts_account }
70
- let!(:group) { FactoryGirl.create :openstax_accounts_group }
71
-
72
- it 'makes api call to groups create' do
73
- ::Api::GroupsController.last_action = nil
74
- ::Api::GroupsController.last_json = nil
75
- Accounts.create_group(account, group)
76
- expect(::Api::GroupsController.last_action).to eq :create
77
- expect(::Api::GroupsController.last_json).to include(
78
- {'name' => 'MyGroup', 'is_public' => false})
79
- end
80
-
81
- it 'makes api call to group update' do
82
- group.save!
83
- ::Api::GroupsController.last_action = nil
84
- ::Api::GroupsController.last_params = nil
85
- ::Api::GroupsController.last_json = nil
86
- Accounts.update_group(account, group)
87
- expect(::Api::GroupsController.last_action).to eq :update
88
- expect(::Api::GroupsController.last_params).to include(
89
- {'id' => group.openstax_uid.to_s})
90
- expect(::Api::GroupsController.last_json).to include(
91
- {'name' => group.name, 'is_public' => group.is_public})
92
- end
93
-
94
- it 'makes api call to group destroy' do
95
- group.save!
96
- ::Api::GroupsController.last_action = nil
97
- ::Api::GroupsController.last_params = nil
98
- Accounts.destroy_group(account, group)
99
- expect(::Api::GroupsController.last_action).to eq :destroy
100
- expect(::Api::GroupsController.last_params).to include(
101
- {'id' => group.openstax_uid.to_s})
102
- end
103
-
104
- end
105
-
106
- context 'group_members' do
107
-
108
- let!(:account) { FactoryGirl.create :openstax_accounts_account }
109
- let!(:group_member) { FactoryGirl.build :openstax_accounts_group_member }
110
-
111
- it 'makes api call to group_members create' do
112
- ::Api::GroupMembersController.last_action = nil
113
- ::Api::GroupMembersController.last_params = nil
114
- Accounts.create_group_member(account, group_member)
115
- expect(::Api::GroupMembersController.last_action).to eq :create
116
- expect(::Api::GroupMembersController.last_params).to include(
117
- {'group_id' => group_member.group_id.to_s,
118
- 'user_id' => group_member.user_id.to_s})
119
- end
120
-
121
- it 'makes api call to group_member destroy' do
122
- group_member.save!
123
- ::Api::GroupMembersController.last_action = nil
124
- ::Api::GroupMembersController.last_params = nil
125
- Accounts.destroy_group_member(account, group_member)
126
- expect(::Api::GroupMembersController.last_action).to eq :destroy
127
- expect(::Api::GroupMembersController.last_params).to include(
128
- {'group_id' => group_member.group_id.to_s,
129
- 'user_id' => group_member.user_id.to_s})
130
- end
131
-
132
- end
133
-
134
- context 'group_owners' do
135
-
136
- let!(:account) { FactoryGirl.create :openstax_accounts_account }
137
- let!(:group_owner) { FactoryGirl.build :openstax_accounts_group_owner }
138
-
139
- it 'makes api call to group_owners create' do
140
- ::Api::GroupOwnersController.last_action = nil
141
- ::Api::GroupOwnersController.last_params = nil
142
- Accounts.create_group_owner(account, group_owner)
143
- expect(::Api::GroupOwnersController.last_action).to eq :create
144
- expect(::Api::GroupOwnersController.last_params).to include(
145
- {'group_id' => group_owner.group_id.to_s,
146
- 'user_id' => group_owner.user_id.to_s})
147
- end
148
-
149
- it 'makes api call to group_owner destroy' do
150
- group_owner.save!
151
- ::Api::GroupOwnersController.last_action = nil
152
- ::Api::GroupOwnersController.last_params = nil
153
- Accounts.destroy_group_owner(account, group_owner)
154
- expect(::Api::GroupOwnersController.last_action).to eq :destroy
155
- expect(::Api::GroupOwnersController.last_params).to include(
156
- {'group_id' => group_owner.group_id.to_s,
157
- 'user_id' => group_owner.user_id.to_s})
158
- end
159
-
160
- end
161
-
162
- context 'group_nestings' do
163
-
164
- let!(:account) { FactoryGirl.create :openstax_accounts_account }
165
- let!(:group_nesting) { FactoryGirl.build :openstax_accounts_group_nesting }
166
-
167
- it 'makes api call to group_nestings (create)' do
168
- ::Api::GroupNestingsController.last_action = nil
169
- ::Api::GroupNestingsController.last_params = nil
170
- Accounts.create_group_nesting(account, group_nesting)
171
- expect(::Api::GroupNestingsController.last_action).to eq :create
172
- expect(::Api::GroupNestingsController.last_params).to include(
173
- {'group_id' => group_nesting.container_group_id.to_s,
174
- 'member_group_id' => group_nesting.member_group_id.to_s})
175
- end
176
-
177
- it 'makes api call to group_nesting (destroy)' do
178
- group_nesting.save!
179
- ::Api::GroupNestingsController.last_action = nil
180
- ::Api::GroupNestingsController.last_params = nil
181
- Accounts.destroy_group_nesting(account, group_nesting)
182
- expect(::Api::GroupNestingsController.last_action).to eq :destroy
183
- expect(::Api::GroupNestingsController.last_params).to include(
184
- {'group_id' => group_nesting.container_group_id.to_s,
185
- 'member_group_id' => group_nesting.member_group_id.to_s})
186
- end
187
-
188
- end
189
-
190
- context 'application_groups' do
191
-
192
- it 'makes api call to application_groups_updates' do
193
- ::Api::ApplicationGroupsController.last_action = nil
194
- Accounts.get_application_group_updates
195
- expect(::Api::ApplicationGroupsController.last_action).to eq :updates
196
- end
197
-
198
- it 'makes api call to application_groups_updated' do
199
- ::Api::ApplicationGroupsController.last_action = nil
200
- ::Api::ApplicationGroupsController.last_json = nil
201
- Accounts.mark_group_updates_as_read([{id: 1, read_updates: 1}])
202
- expect(::Api::ApplicationGroupsController.last_action).to eq :updated
203
- expect(::Api::ApplicationGroupsController.last_json).to include(
204
- {'id' => 1, 'read_updates' => 1})
205
- end
206
-
207
- end
208
-
209
- end
210
- end