canvas_oauth_engine 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +102 -0
  3. data/Rakefile +28 -0
  4. data/app/controllers/canvas_oauth/application_controller.rb +5 -0
  5. data/app/controllers/canvas_oauth/canvas_controller.rb +25 -0
  6. data/app/models/canvas_oauth/authorization.rb +26 -0
  7. data/config/canvas.yml.example +12 -0
  8. data/config/routes.rb +3 -0
  9. data/db/migrate/20121121005358_create_canvas_oauth_authorizations.rb +15 -0
  10. data/lib/canvas_oauth.rb +25 -0
  11. data/lib/canvas_oauth/canvas_api.rb +249 -0
  12. data/lib/canvas_oauth/canvas_api_extensions.rb +8 -0
  13. data/lib/canvas_oauth/canvas_application.rb +66 -0
  14. data/lib/canvas_oauth/canvas_config.rb +28 -0
  15. data/lib/canvas_oauth/config.rb +1 -0
  16. data/lib/canvas_oauth/engine.rb +15 -0
  17. data/lib/canvas_oauth/version.rb +3 -0
  18. data/lib/tasks/canvas_oauth_tasks.rake +1 -0
  19. data/spec/controllers/canvas_oauth/canvas_controller_spec.rb +80 -0
  20. data/spec/dummy/README.rdoc +261 -0
  21. data/spec/dummy/Rakefile +7 -0
  22. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  23. data/spec/dummy/app/controllers/welcome_controller.rb +5 -0
  24. data/spec/dummy/config.ru +4 -0
  25. data/spec/dummy/config/application.rb +55 -0
  26. data/spec/dummy/config/boot.rb +10 -0
  27. data/spec/dummy/config/canvas.yml +12 -0
  28. data/spec/dummy/config/database.yml +25 -0
  29. data/spec/dummy/config/environment.rb +5 -0
  30. data/spec/dummy/config/environments/development.rb +26 -0
  31. data/spec/dummy/config/environments/production.rb +69 -0
  32. data/spec/dummy/config/environments/test.rb +33 -0
  33. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/spec/dummy/config/initializers/inflections.rb +15 -0
  35. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  36. data/spec/dummy/config/initializers/secret_token.rb +8 -0
  37. data/spec/dummy/config/initializers/session_store.rb +8 -0
  38. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  39. data/spec/dummy/config/locales/en.yml +5 -0
  40. data/spec/dummy/config/routes.rb +4 -0
  41. data/spec/dummy/db/migrate/20130326194409_create_canvas_oauth_authorizations.canvas_oauth.rb +13 -0
  42. data/spec/dummy/db/schema.rb +25 -0
  43. data/spec/dummy/db/test.sqlite3 +0 -0
  44. data/spec/dummy/log/test.log +8127 -0
  45. data/spec/dummy/public/404.html +26 -0
  46. data/spec/dummy/public/422.html +26 -0
  47. data/spec/dummy/public/500.html +25 -0
  48. data/spec/dummy/public/favicon.ico +0 -0
  49. data/spec/dummy/public/robots.txt +5 -0
  50. data/spec/dummy/script/rails +6 -0
  51. data/spec/lib/canvas_oauth/canvas_api_extensions_spec.rb +13 -0
  52. data/spec/lib/canvas_oauth/canvas_api_spec.rb +228 -0
  53. data/spec/models/canvas_oauth/authorization_spec.rb +47 -0
  54. data/spec/spec_helper.rb +57 -0
  55. metadata +353 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe CanvasOauth::CanvasApiExtensions do
4
+ describe "build" do
5
+ subject { CanvasOauth::CanvasApiExtensions.build('http://test.canvas', 1, 'abc123') }
6
+
7
+ before { allow(CanvasOauth::Authorization).to receive(:fetch_token).with(1, 'abc123').and_return('token') }
8
+
9
+ it { is_expected.to be_a CanvasOauth::CanvasApi }
10
+ its(:token) { is_expected.to eq 'token' }
11
+ its(:canvas_url) { is_expected.to eq 'http://test.canvas' }
12
+ end
13
+ end
@@ -0,0 +1,228 @@
1
+ require 'spec_helper'
2
+
3
+ describe CanvasOauth::CanvasApi do
4
+ let(:canvas) { CanvasOauth::CanvasApi.new('http://test.canvas', 'token', 'key', 'secret') }
5
+
6
+ describe "initializer" do
7
+ subject { canvas }
8
+
9
+ its(:canvas_url) { is_expected.to eq 'http://test.canvas' }
10
+ its(:token) { is_expected.to eq 'token' }
11
+ its(:key) { is_expected.to eq 'key' }
12
+ its(:secret) { is_expected.to eq 'secret' }
13
+ end
14
+
15
+ describe "auth_url" do
16
+ subject { canvas.auth_url('http://localhost:3001/canvas/oauth', 'zzxxyy') }
17
+
18
+ it { is_expected.to eq "http://test.canvas/login/oauth2/auth?client_id=key&response_type=code&state=zzxxyy&redirect_uri=http://localhost:3001/canvas/oauth" }
19
+ end
20
+
21
+ describe "get_access_token" do
22
+ it "POSTs to /login/oauth2/token" do
23
+ expect(CanvasOauth::CanvasApi).to receive(:post).with('/login/oauth2/token', anything()).and_return({})
24
+ canvas.get_access_token('code')
25
+ end
26
+
27
+ it "returns the access token" do
28
+ allow(CanvasOauth::CanvasApi).to receive(:post).and_return({ 'access_token' => 'token' })
29
+ expect(canvas.get_access_token('code')).to eq 'token'
30
+ end
31
+
32
+ it "sends the key, secret, and code as params" do
33
+ params = {
34
+ body: {
35
+ client_id: 'key',
36
+ client_secret: 'secret',
37
+ code: 'code'
38
+ }
39
+ }
40
+
41
+ expect(CanvasOauth::CanvasApi).to receive(:post).with(anything(), params).and_return({})
42
+ canvas.get_access_token('code')
43
+ end
44
+ end
45
+
46
+ describe "requests" do
47
+ describe "authenticated_request" do
48
+ it "passes the params along as-is and adds an Authorization header" do
49
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/path', { query: 'stuff', headers: { 'Authorization' => 'Bearer token' } })
50
+ canvas.authenticated_request :get, '/path', { query: 'stuff' }
51
+ end
52
+
53
+ it "raises an authenticate error when the response is a 401 and WWW-Authenticate is set" do
54
+ allow(CanvasOauth::CanvasApi).to receive(:get).and_return(double(unauthorized?: true, headers: { 'WWW-Authenticate' => true }))
55
+ expect { canvas.authenticated_request :get, '/path' }.to raise_error CanvasOauth::CanvasApi::Authenticate
56
+ end
57
+
58
+ it "raises an unauthorized error when the response is a 401" do
59
+ allow(CanvasOauth::CanvasApi).to receive(:get).and_return(double(unauthorized?: true, headers: {}))
60
+ expect { canvas.authenticated_request :get, '/path' }.to raise_error CanvasOauth::CanvasApi::Unauthorized
61
+ end
62
+ end
63
+
64
+ describe "get_courses" do
65
+ it "queries /api/v1/courses" do
66
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/courses', anything())
67
+ canvas.get_courses
68
+ end
69
+ end
70
+
71
+ describe "get_account_courses" do
72
+ it "queries /api/v1/accounts/:id/courses" do
73
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/accounts/1/courses', anything())
74
+ canvas.get_account_courses(1)
75
+ end
76
+
77
+ it "paginates" do
78
+ expect(canvas).to receive(:paginated_get)
79
+ canvas.get_account_courses(1)
80
+ end
81
+ end
82
+
83
+ describe "get account users" do
84
+ it "queries /api/v1/accounts/:id/users" do
85
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/accounts/1/users', anything())
86
+ canvas.get_account_users(1)
87
+ end
88
+
89
+ it "paginates" do
90
+ expect(canvas).to receive(:paginated_get)
91
+ canvas.get_account_users(1)
92
+ end
93
+ end
94
+
95
+ describe "get_course" do
96
+ it "queries /api/v1/courses/:id" do
97
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/courses/123', anything())
98
+ canvas.get_course('123')
99
+ end
100
+ end
101
+
102
+ describe "get_course_students" do
103
+ it "queries /api/v1/courses/:id/students" do
104
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/courses/123/students', anything())
105
+ canvas.get_course_students('123')
106
+ end
107
+
108
+ it "paginates" do
109
+ expect(canvas).to receive(:paginated_get)
110
+ canvas.get_course_students('123')
111
+ end
112
+ end
113
+
114
+ describe "get_sections" do
115
+ it "queries /api/v1/courses/:id/sections" do
116
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/courses/123/sections', anything())
117
+ canvas.get_sections('123')
118
+ end
119
+
120
+ it "paginates" do
121
+ expect(canvas).to receive(:paginated_get)
122
+ canvas.get_sections('123')
123
+ end
124
+ end
125
+
126
+ describe "get_assignments" do
127
+ it "queries /api/v1/courses/:id/assignments" do
128
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/courses/123/assignments', anything())
129
+ canvas.get_assignments('123')
130
+ end
131
+
132
+ it "paginates" do
133
+ expect(canvas).to receive(:paginated_get)
134
+ canvas.get_account_courses('123')
135
+ end
136
+ end
137
+
138
+ describe "get_user_profile" do
139
+ it "queries /api/v1/users/:id/profile" do
140
+ expect(CanvasOauth::CanvasApi).to receive(:get).with('/api/v1/users/123/profile', anything())
141
+ canvas.get_user_profile('123')
142
+ end
143
+ end
144
+
145
+ describe "create_assignment" do
146
+ it "posts to /api/v1/courses/:id/assignments" do
147
+ expect(CanvasOauth::CanvasApi).to receive(:post).with('/api/v1/courses/123/assignments', anything())
148
+ canvas.create_assignment('123', name: "Assignment")
149
+ end
150
+
151
+ it "sets the body of the request to the assignment params" do
152
+ expect(canvas).to receive(:authenticated_post).with(anything(), { body: { assignment: { name: "Assignment" }}})
153
+ canvas.create_assignment('123', name: "Assignment")
154
+ end
155
+ end
156
+
157
+ describe "grade_assignment" do
158
+ it "puts to /api/v1/courses/:course_id/assignments/:assignment_id/submissions/:id" do
159
+ expect(CanvasOauth::CanvasApi).to receive(:put).with('/api/v1/courses/1/assignments/2/submissions/3', anything())
160
+ canvas.grade_assignment('1', '2', '3', {})
161
+ end
162
+
163
+ it "sets the body of the request to the grade params" do
164
+ expect(canvas).to receive(:authenticated_put).with(anything(), { body: { percentage: "80%" }})
165
+ canvas.grade_assignment('1', '2', '3', percentage: "80%")
166
+ end
167
+ end
168
+ end
169
+
170
+ describe "pagination" do
171
+ describe "valid_page?" do
172
+ let(:valid_page) { double(size: 2, nil?: false, body: '[{some:json}]') }
173
+ let(:same_page) { valid_page }
174
+ let(:blank_page) { double(size: 0, nil?: false, body: '[]') }
175
+
176
+ specify { expect(canvas.valid_page?(nil)).to be_falsey }
177
+ specify { expect(canvas.valid_page?(valid_page)).to be_truthy }
178
+ specify { expect(canvas.valid_page?(blank_page)).to be_falsey }
179
+ end
180
+
181
+ describe "paginated_get" do
182
+ it "adds per_page parameters to the request query" do
183
+ expect(canvas).to receive(:authenticated_get).with("/some/address", query: { per_page: 50 })
184
+ canvas.paginated_get "/some/address"
185
+ end
186
+
187
+ it "requests the next link" do
188
+ allow(canvas).to receive(:valid_page?) { true }
189
+ first_response = []
190
+ second_response = []
191
+ allow(first_response).to receive(:headers).and_return({'link' => "<https://foobar.com/some/address?page=2>; rel=\"next\", <https://foobar.com/some/address?page=2>; rel=\"last\""})
192
+ allow(second_response).to receive(:headers).and_return({})
193
+ expect(canvas).to receive(:authenticated_get).
194
+ exactly(2).times.and_return(first_response,second_response)
195
+ canvas.paginated_get "/some/address"
196
+ end
197
+
198
+ it "sends only one request when no next link is in the response Link header" do
199
+ allow(canvas).to receive(:valid_page?) { true }
200
+ response = [{totally: "A real fake response"}]
201
+ allow(response).to receive(:headers).and_return({})
202
+ expect(canvas).to receive(:authenticated_get).once.and_return(response)
203
+ canvas.paginated_get "/some/address"
204
+ end
205
+
206
+ it "sends just one request when an invalid result is returned" do
207
+ allow(canvas).to receive(:valid_page?) { false }
208
+ response = []
209
+ allow(response).to receive(:headers).and_return({})
210
+ expect(canvas).to receive(:authenticated_get).once.and_return(response)
211
+ canvas.paginated_get "/some/address"
212
+ end
213
+ end
214
+ end
215
+
216
+ describe "course_account_id" do
217
+ it "returns the 'account_id' of a course" do
218
+ allow(canvas).to receive(:get_course).with(1).and_return('account_id' => 3)
219
+ expect(canvas.course_account_id(1)).to eq 3
220
+ end
221
+ end
222
+
223
+ describe "hex_sis_id" do
224
+ it "encodes the passed in ID and creates an SIS ID string" do
225
+ expect(canvas.hex_sis_id("sis_course_id", "101")).to eq "hex:sis_course_id:313031"
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe CanvasOauth::Authorization do
4
+ it { is_expected.to validate_presence_of :canvas_user_id }
5
+ it { is_expected.to validate_presence_of :token }
6
+ it { is_expected.to validate_presence_of :last_used_at }
7
+
8
+ describe "cache_token" do
9
+ subject { CanvasOauth::Authorization.first }
10
+
11
+ before do
12
+ CanvasOauth::Authorization.cache_token('abc', 123, 'abc123')
13
+ end
14
+
15
+ its(:token) { is_expected.to eq 'abc' }
16
+ its(:canvas_user_id) { is_expected.to eq 123 }
17
+ its(:tool_consumer_instance_guid) { is_expected.to eq 'abc123' }
18
+ its(:last_used_at) { is_expected.to be_present }
19
+ end
20
+
21
+ describe "fetch_token" do
22
+ subject(:token) { CanvasOauth::Authorization.fetch_token(123, 'abc123') }
23
+
24
+ context "when a token exists" do
25
+ before do
26
+ CanvasOauth::Authorization.cache_token('abc', 123, 'abc123')
27
+ CanvasOauth::Authorization.cache_token('def', 123, 'abc123')
28
+ end
29
+
30
+ it "retrieves the latest one" do
31
+ expect(token).to eq 'def'
32
+ end
33
+ end
34
+
35
+ context "when a token exists with a tool_consumer_instance_guid" do
36
+ before do
37
+ CanvasOauth::Authorization.cache_token('abc', 123, 'wrong')
38
+ end
39
+
40
+ it { is_expected.to be_nil }
41
+ end
42
+
43
+ context "when no token exists" do
44
+ it { is_expected.to be_nil }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,57 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+ require File.expand_path("../dummy/config/environment", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'rspec/its'
6
+ require 'webmock/rspec'
7
+ require 'shoulda/matchers'
8
+
9
+ # Requires supporting ruby files with custom matchers and macros, etc,
10
+ # in spec/support/ and its subdirectories.
11
+ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
12
+
13
+ module CanvasOauth
14
+ module UrlHelpers
15
+ def canvas_oauth
16
+ ::CanvasOauth::Engine.routes.url_helpers
17
+ end
18
+
19
+ def main_app
20
+ ::Dummy::Application.routes.url_helpers
21
+ end
22
+ end
23
+ end
24
+
25
+ RSpec.configure do |config|
26
+ # ## Mock Framework
27
+ #
28
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
29
+ #
30
+ # config.mock_with :mocha
31
+ # config.mock_with :flexmock
32
+ # config.mock_with :rr
33
+
34
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
35
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
36
+
37
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
38
+ # examples within a transaction, remove the following line or assign false
39
+ # instead of true.
40
+ config.use_transactional_fixtures = true
41
+
42
+ # If true, the base class of anonymous controllers will be inferred
43
+ # automatically. This will be the default behavior in future versions of
44
+ # rspec-rails.
45
+ config.infer_base_class_for_anonymous_controllers = false
46
+
47
+ # Automatically infer an example group's spec type from the file location
48
+ config.infer_spec_type_from_file_location!
49
+
50
+ # Run specs in random order to surface order dependencies. If you find an
51
+ # order dependency and want to debug it, you can fix the order by providing
52
+ # the seed, which is printed after each run.
53
+ # --seed 1234
54
+ config.order = "random"
55
+
56
+ config.include CanvasOauth::UrlHelpers, type: :controller
57
+ end