Skip to content

Commit 0909266

Browse files
authored
Merge pull request #2258 from openstax/bootstrap-data
serve bootstrap data from api endpoint vs html
2 parents f525510 + 84e047a commit 0909266

File tree

7 files changed

+56
-26
lines changed

7 files changed

+56
-26
lines changed

app/controllers/api/v1/users_controller.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Api::V1::UsersController < Api::V1::ApiController
2+
23
resource_description do
34
api_versions "v1"
45
short_description 'Represents a user in the system'
@@ -22,6 +23,27 @@ def show
2223
end
2324
end
2425

26+
27+
api :GET, '/bootstrap', 'Returns initial of data application needs to start up'
28+
description <<-EOS
29+
Includes user information, current courses, and terms of use.
30+
#{json_schema(Api::V1::BootstrapDataRepresenter, include: :readable)}
31+
EOS
32+
def bootstrap
33+
if current_human_user.nil? || current_human_user.is_anonymous?
34+
head :forbidden
35+
else
36+
respond_with current_human_user,
37+
represent_with: Api::V1::BootstrapDataRepresenter,
38+
user_options: {
39+
tutor_api_url: api_root_url,
40+
flash: flash.to_hash,
41+
is_impersonating: session[:admin_user_id].present?
42+
}
43+
end
44+
end
45+
46+
2547
api :PUT, '/ui-settings', 'Save settings about the currently logged in user'
2648
description <<-EOS
2749
Saves the user interface settigs for the current user.

app/helpers/webview_helper.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/views/layouts/webview.html.erb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
<%= render 'webview/google_analytics' %>
1313
</head>
1414
<body>
15-
<script id="tutor-boostrap-data" type="application/json"><%=raw json_escape bootstrap_data %></script>
1615
<%= yield %>
17-
1816
</body>
1917
</html>

app/views/lms/pair.html.erb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
</head>
1010
<body>
1111
<div id="ox-root-view">
12-
<script id="tutor-boostrap-data" type="application/json"><%=raw json_escape bootstrap_data %></script>
1312
</div>
1413
</body>
1514
</html>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636

3737
# All API routes
3838
api :v1, default: true do
39+
3940
resources :users, only: :index
4041
resource :user, only: :show do
42+
get :bootstrap
4143
get :tasks
4244
put :ui_settings
4345
put :'tours/:tour_id', action: :record_tour_view

spec/requests/api/v1/users_controller_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,38 @@
4646
end
4747
end
4848

49+
context "#bootstrap" do
50+
context "caller has an authorization token" do
51+
it "should return an ok (200) code" do
52+
api_get bootstrap_api_user_url, user_1_token
53+
expect(response.code).to eq('200')
54+
expect(response.body_as_hash).to(
55+
match Api::V1::BootstrapDataRepresenter.new(user_1).to_hash(
56+
user_options: {
57+
tutor_api_url: api_root_url,
58+
flash: {},
59+
is_impersonating: false,
60+
}
61+
).deep_symbolize_keys
62+
)
63+
end
64+
end
65+
66+
context "caller does not have an authorization token" do
67+
it "should return a forbidden (403) code" do
68+
api_get bootstrap_api_user_url, nil
69+
expect(response.code).to eq('403')
70+
end
71+
end
72+
73+
context "caller has an application/client credentials authorization token" do
74+
it "should return a forbidden (403) code" do
75+
api_get bootstrap_api_user_url, userless_token
76+
expect(response.code).to eq('403')
77+
end
78+
end
79+
end
80+
4981
context "#ui-settings" do
5082
it "saves to profile" do
5183
api_put ui_settings_api_user_url, user_1_token, params: {

spec/requests/webview_controller_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@
4747
context 'as a signed in user' do
4848
before { sign_in! registered_user }
4949

50-
it 'sets boostrap data in script tag' do
51-
get "/#{SecureRandom.hex}", headers: headers
52-
expect(response).to have_http_status(:success)
53-
doc = Nokogiri::HTML(response.body)
54-
data = ::JSON.parse(doc.css('body script#tutor-boostrap-data').inner_text)
55-
expect(data).to include(
56-
'courses'=> CollectCourseInfo[user: registered_user].as_json,
57-
'user' => Api::V1::UserRepresenter.new(registered_user).as_json
58-
)
59-
end
60-
6150
it 'has url to tutor js asset' do
6251
get "/#{SecureRandom.hex}", headers: headers
6352
expect(response.body).to include "src='#{OpenStax::Utilities::Assets.url_for('tutor.js')}'"

0 commit comments

Comments
 (0)