Lookup asciicast creator on controller level

This commit is contained in:
Marcin Kulik 2015-02-26 16:17:11 +00:00
parent 87d60054cf
commit 78a2ba9ea9
3 changed files with 13 additions and 76 deletions

View File

@ -30,14 +30,20 @@ module Api
private private
def parse_request def parse_request
attrs, username, token = parse_format_0_request
user = User.for_api_token!(token, username)
[attrs, user]
end
def parse_format_0_request
meta = JSON.parse(params[:asciicast][:meta].read) meta = JSON.parse(params[:asciicast][:meta].read)
username, token = authenticate_with_http_basic { |username, password| [username, password] } username, token = authenticate_with_http_basic { |username, password| [username, password] }
token ||= meta.delete('user_token')
username ||= meta.delete('username')
attrs = AsciicastParams.build(params[:asciicast].merge(meta: meta), request.user_agent)
[ [attrs, username, token]
AsciicastParams.build(params[:asciicast].merge(meta: meta), request.user_agent),
token || meta.delete('user_token'),
username || meta.delete('username'),
]
end end
def asciicast_creator def asciicast_creator

View File

@ -1,9 +1,7 @@
class AsciicastCreator class AsciicastCreator
def create(attributes, token, username) def create(attributes, user)
user = User.for_api_token!(token, username) asciicast = Asciicast.create!(attributes.merge(user: user))
attributes = attributes.merge(user: user)
asciicast = Asciicast.create!(attributes)
AsciicastWorker.perform_async(asciicast.id) AsciicastWorker.perform_async(asciicast.id)
asciicast asciicast

View File

@ -1,67 +0,0 @@
require 'rails_helper'
module Api
describe AsciicastsController do
describe '#create' do
subject { post :create, asciicast: attributes }
let(:meta) {
ActionDispatch::Http::UploadedFile.new(
filename: 'meta.json',
type: 'application/json',
tempfile: StringIO.new('{ "username": "lol", "user_token": "token" }'),
)
}
let(:attributes) { { 'title' => 'bar', 'meta' => meta } }
let(:creator) { double('creator') }
before do
request.headers['User-Agent'] = 'Smith'
allow(controller).to receive(:asciicast_creator) { creator }
allow(AsciicastParams).to receive(:build) { { title: 'bar', user_agent: 'Smith' } }
end
context 'when the creator returns an asciicast' do
let(:asciicast) { stub_model(Asciicast, id: 666) }
before do
allow(creator).to receive(:create).
with({ title: 'bar', user_agent: 'Smith' }, 'token', 'lol') { asciicast }
subject
end
it 'returns the status 201' do
expect(response.status).to eq(201)
end
it 'returns the URL of created asciicast as the content body' do
expect(response.body).to eq(asciicast_url(asciicast))
end
end
context 'when the creator raises ActiveRecord::RecordInvalid' do
let(:asciicast) { stub_model(Asciicast, errors: errors) }
let(:errors) { double('errors', full_messages: full_messages) }
let(:full_messages) { ['This is invalid'] }
before do
allow(creator).to receive(:create).
with({ title: 'bar', user_agent: 'Smith' }, 'token', 'lol').
and_raise(ActiveRecord::RecordInvalid.new(asciicast))
post :create, asciicast: attributes
end
it 'returns the status 422' do
expect(response.status).to eq(422)
end
it 'returns nothing as the content body' do
expect(response.body).to be_blank
end
end
end
end
end