Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add endpoint for the last hour's submissions
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/controllers/api/v8/courses/submissions/last_hour_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Api | ||
module V8 | ||
module Courses | ||
module Submissions | ||
class LastHourController < Api::V8::BaseController | ||
include Swagger::Blocks | ||
|
||
swagger_path '/api/v8/courses/{course_id}/submissions/last_hour' do | ||
operation :get do | ||
key :description, 'Returns submissions to the course in the latest hour' | ||
key :produces, ['application/json'] | ||
key :tags, ['submission'] | ||
parameter '$ref': '#/parameters/path_course_id' | ||
response 403, '$ref': '#/responses/error' | ||
response 404, '$ref': '#/responses/error' | ||
response 200 do | ||
key :description, 'Submission ids' | ||
schema do | ||
key :type, :array | ||
items do | ||
key :type, :integer | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
skip_authorization_check | ||
|
||
def index | ||
return respond_access_denied unless current_user.administrator? | ||
course = Course.find(params[:course_id]) | ||
|
||
current_time = Time.current | ||
ids = course.submissions.where(created_at: (current_time - 1.hour)..current_time).ids | ||
present(ids) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
spec/controllers/api/v8/courses/submissions/last_hour_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
require 'json' | ||
|
||
describe Api::V8::Courses::Submissions::LastHourController, type: :controller do | ||
let!(:course) { FactoryGirl.create(:course) } | ||
let(:regular_user) { FactoryGirl.create(:user) } | ||
let(:admin_user) { FactoryGirl.create(:admin) } | ||
|
||
let!(:submission_1) { FactoryGirl.create(:submission, course: course) } | ||
let!(:submission_2) { FactoryGirl.create(:submission, course: course) } | ||
let!(:submission_3) { FactoryGirl.create(:submission, course: course, created_at: Time.current - 2.hours) } | ||
|
||
before(:each) do | ||
controller.stub(:doorkeeper_token) { token } | ||
end | ||
|
||
describe 'index' do | ||
context 'as a regular user' do | ||
let!(:token) { double resource_owner_id: regular_user.id, acceptable?: true } | ||
|
||
it 'should not allow access' do | ||
get :index, course_id: course.id | ||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
end | ||
|
||
context 'as an admin' do | ||
let!(:token) { double resource_owner_id: admin_user.id, acceptable?: true } | ||
|
||
it 'gives submission ids for the last hour' do | ||
get :index, course_id: course.id | ||
expect(response).to have_http_status(:ok) | ||
res = JSON.parse(response.body) | ||
expect(res.length).to eq(2) | ||
expect(res).to include(submission_1.id) | ||
expect(res).not_to include(submission_3.id) | ||
end | ||
end | ||
end | ||
end |