Move asciicast attributes preparation to AsciicastAttributes

This commit is contained in:
Marcin Kulik 2013-09-25 19:29:57 +02:00
parent f3ecd376d7
commit d71da5b369
7 changed files with 146 additions and 147 deletions

View File

@ -28,8 +28,6 @@ class Asciicast < ActiveRecord::Base
popular.includes(:user).page(page).per(per_page)
end)
before_create :assign_user, :unless => :user # TODO: move this AsciicastCreator
attr_accessible :title, :description, :time_compression
def self.cache_key
@ -37,15 +35,6 @@ class Asciicast < ActiveRecord::Base
Digest::MD5.hexdigest timestamps.join('/')
end
def assign_user
if user_token.present?
if ut = UserToken.find_by_token(user_token)
self.user = ut.user
self.user_token = nil
end
end
end
def stdout
@stdout ||= BufferedStdout.new(stdout_data.decompressed_path,
stdout_timing.decompressed_path).lazy

View File

@ -0,0 +1,52 @@
class AsciicastParams
def initialize(input)
@input = input
end
def to_h
attributes = {
:stdout_data => input[:stdout],
:stdout_timing => input[:stdout_timing],
:stdin_data => input[:stdin],
:stdin_timing => input[:stdin_timing],
:username => meta['username'],
:duration => meta['duration'],
:recorded_at => meta['recorded_at'],
:title => meta['title'],
:command => meta['command'],
:shell => meta['shell'],
:uname => meta['uname'],
:terminal_lines => meta['term']['lines'],
:terminal_columns => meta['term']['columns'],
:terminal_type => meta['term']['type'],
}
assign_user_or_token(attributes, meta)
attributes
end
private
attr_reader :input
def meta
@meta ||= JSON.parse(input[:meta].read)
end
def assign_user_or_token(attributes, meta)
token = meta['user_token']
if token.present?
user_token = UserToken.find_by_token(token)
if user_token
attributes[:user_id] = user_token.user_id
else
attributes[:user_token] = token
end
end
end
end

View File

@ -1,40 +1,11 @@
class AsciicastCreator
def create(attributes)
attributes = prepare_attributes(attributes)
options = { :without_protection => true }
attributes = AsciicastParams.new(attributes).to_h
asciicast = Asciicast.create!(attributes, without_protection: true)
AsciicastWorker.perform_async(asciicast.id)
Asciicast.create!(attributes, options).tap do |asciicast|
AsciicastWorker.perform_async(asciicast.id)
end
end
private
def prepare_attributes(attributes)
meta = parse_meta_file(attributes[:meta])
{
:stdout_data => attributes[:stdout],
:stdout_timing => attributes[:stdout_timing],
:stdin_data => attributes[:stdin],
:stdin_timing => attributes[:stdin_timing],
:username => meta['username'],
:user_token => meta['user_token'],
:duration => meta['duration'],
:recorded_at => meta['recorded_at'],
:title => meta['title'],
:command => meta['command'],
:shell => meta['shell'],
:uname => meta['uname'],
:terminal_lines => meta['term']['lines'],
:terminal_columns => meta['term']['columns'],
:terminal_type => meta['term']['type'],
}
end
def parse_meta_file(file)
JSON.parse(file.read)
asciicast
end
end

14
spec/fixtures/meta-no-token.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"command": "/bin/bash",
"duration": 11.146430015563965,
"recorded_at": "Thu, 25 Jul 2013 20:08:57 +0000",
"shell": "/bin/zsh",
"term": {
"columns": 96,
"lines": 26,
"type": "screen-256color"
},
"title": "bashing :)",
"uname": "Linux 3.9.9-302.fc19.x86_64 #1 SMP Sat Jul 6 13:41:07 UTC 2013 x86_64",
"username": "kill"
}

View File

@ -0,0 +1,62 @@
require 'spec_helper'
describe AsciicastParams do
let(:asciicast_params) { described_class.new(input) }
let(:input) { {
:meta => meta_file,
:stdout => stdout_data_file,
:stdout_timing => stdout_timing_file
} }
let(:stdout_data_file) { double('stdout_data_file') }
let(:stdout_timing_file) { double('stdout_timing_file') }
describe '#to_h' do
let(:expected_attrs) { {
:stdout_data => stdout_data_file,
:stdout_timing => stdout_timing_file,
:stdin_data => nil,
:stdin_timing => nil,
:username => 'kill',
:duration => 11.146430015563965,
:recorded_at => 'Thu, 25 Jul 2013 20:08:57 +0000',
:title => 'bashing :)',
:command => '/bin/bash',
:shell => '/bin/zsh',
:uname => 'Linux 3.9.9-302.fc19.x86_64 #1 SMP ' +
'Sat Jul 6 13:41:07 UTC 2013 x86_64',
:terminal_columns => 96,
:terminal_lines => 26,
:terminal_type => 'screen-256color'
} }
subject { asciicast_params.to_h }
context "when no user_token given" do
let(:meta_file) { fixture_file_upload('spec/fixtures/meta-no-token.json',
'application/json') }
it { should eq(expected_attrs) }
end
context "when user_token given" do
let(:meta_file) { fixture_file_upload('spec/fixtures/meta.json',
'application/json') }
let(:token) { 'f33e6188-f53c-11e2-abf4-84a6c827e88b' }
context "and user with this token exists" do
let(:user) { create(:user) }
let!(:user_token) { create(:user_token, token: token, user: user) }
it { should eq(expected_attrs.merge(user_id: user.id)) }
end
context "and user with this token doesn't exist" do
it { should eq(expected_attrs.merge(user_token: token)) }
end
end
end
end

View File

@ -3,75 +3,7 @@ require 'tempfile'
describe Asciicast do
let(:asciicast) { Asciicast.new }
describe '.assign_user' do
let(:user) { FactoryGirl.create(:user) }
let(:token) { 'token' }
let!(:asciicast) do
FactoryGirl.create(:asciicast, :user => nil, :user_token => token)
end
subject { Asciicast.assign_user(token, user) }
it 'returns number of updated records' do
subject.should == 1
end
it 'assigns user to matching asciicasts' do
subject
asciicast.reload.user.should == user
end
end
# TODO: create a service for this
describe '#save' do
let(:asciicast) { FactoryGirl.build(:asciicast, :user => user) }
context 'when no user given' do
let(:user) { nil }
it 'calls #assign_user' do
asciicast.should_receive(:assign_user)
asciicast.save
end
end
context 'when user given' do
let(:user) { FactoryGirl.build(:user) }
it "doesn't call #assign_user" do
asciicast.should_not_receive(:assign_user)
asciicast.save
end
end
end
describe '#assign_user' do
let(:user) { FactoryGirl.create(:user) }
let(:asciicast) do
FactoryGirl.create(:asciicast, :user => nil, :user_token => user_token)
end
context 'when user exists with given token' do
let(:user_token) { FactoryGirl.create(:user_token, :user => user).token }
it 'assigns user and resets user_token' do
asciicast.assign_user
asciicast.user.should == user
asciicast.user_token.should be(nil)
end
end
context 'when there is no user with given token' do
let(:user_token) { 'some-foo-bar' }
it 'assigns user' do
asciicast.assign_user
asciicast.user.should be(nil)
end
end
end
let(:asciicast) { described_class.new }
describe '#stdout' do
let(:asciicast) { Asciicast.new }

View File

@ -1,59 +1,38 @@
require 'spec_helper'
describe AsciicastCreator do
let(:creator) { AsciicastCreator.new }
let(:creator) { described_class.new }
describe '#create' do
let(:meta_file) {
fixture_file_upload('spec/fixtures/meta.json', 'application/json')
}
let(:stdout_data_file) { double('stdout_data_file') }
let(:stdout_timing_file) { double('stdout_timing_file') }
let(:asciicast) { stub_model(Asciicast, :id => 666) }
let(:asciicast) { stub_model(Asciicast, id: 666) }
let(:input_attrs) { { a: 'A' } }
let(:prepared_attrs) { { b: 'B' } }
subject {
creator.create(
:meta => meta_file,
:stdout => stdout_data_file,
:stdout_timing => stdout_timing_file
)
}
subject { creator.create(input_attrs) }
before do
allow(AsciicastParams).to receive(:new).
with(input_attrs) { prepared_attrs }
allow(Asciicast).to receive(:create!) { asciicast }
end
it 'calls Asciicast.create! with proper attributes' do
subject
expect(Asciicast).to have_received(:create!).with({
:stdout_data => stdout_data_file,
:stdout_timing => stdout_timing_file,
:stdin_data => nil,
:stdin_timing => nil,
:username => 'kill',
:user_token => 'f33e6188-f53c-11e2-abf4-84a6c827e88b',
:duration => 11.146430015563965,
:recorded_at => 'Thu, 25 Jul 2013 20:08:57 +0000',
:title => 'bashing :)',
:command => '/bin/bash',
:shell => '/bin/zsh',
:uname => 'Linux 3.9.9-302.fc19.x86_64 #1 SMP ' +
'Sat Jul 6 13:41:07 UTC 2013 x86_64',
:terminal_columns => 96,
:terminal_lines => 26,
:terminal_type => 'screen-256color'
}, { :without_protection => true })
expect(Asciicast).to have_received(:create!).
with({ b: 'B' }, { without_protection: true })
end
it 'enqueues snapshot capture job' do
it 'enqueues a post-processing job' do
subject
expect(AsciicastWorker).to have_queued_job(asciicast.id)
expect(AsciicastWorker).to have_queued_job(666)
end
it 'returns the created asciicast' do
expect(subject).to be(asciicast)
end
end
end