diff --git a/spec/api/asciicast_create_spec.rb b/spec/api/asciicast_create_spec.rb index 47aaf64..0606a66 100644 --- a/spec/api/asciicast_create_spec.rb +++ b/spec/api/asciicast_create_spec.rb @@ -2,33 +2,271 @@ require 'rails_helper' describe "Asciicast creation" do - before do - post '/api/asciicasts', asciicast: { - meta: fixture_file(meta_filename, 'application/json'), - stdout: fixture_file('stdout', 'application/octet-stream'), - stdout_timing: fixture_file('stdout.time', 'application/octet-stream') - } + let(:created_asciicast) { Asciicast.last } + + def basic_auth_header(user, password) + { 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password) } + end + + def user_agent_header(user_agent) + { 'User-Agent' => user_agent } + end + + def headers(user, password, user_agent) + h = {} + + h.merge!(basic_auth_header(user, password)) if user + h.merge!(user_agent_header(user_agent)) if user_agent + + h end - let(:meta_filename) { 'meta.json' } + context '<= v0.9.7 client' do + subject { make_request } + + def make_request + post '/api/asciicasts', + { + asciicast: { + meta: fixture_file('0.9.7/meta.json', 'application/json'), + stdout: fixture_file('0.9.7/stdout', 'application/octet-stream'), + stdout_timing: fixture_file('0.9.7/stdout.time', 'application/octet-stream') + } + }, headers(nil, nil, 'python-requests blah/blah') + end + + before { subject } + + it 'creates asciicast with given command' do + expect(created_asciicast.command).to eq('/bin/bash') + end + + it 'creates asciicast with given duration' do + expect(created_asciicast.duration).to eq(11.146430015564) + end + + it 'creates asciicast with given shell' do + expect(created_asciicast.shell).to eq('/bin/zsh') + end + + it 'creates asciicast with given terminal type' do + expect(created_asciicast.terminal_type).to eq('screen-256color') + end + + it 'creates asciicast with given terminal width' do + expect(created_asciicast.terminal_columns).to eq(96) + end + + it 'creates asciicast with given terminal height' do + expect(created_asciicast.terminal_lines).to eq(26) + end + + it 'creates asciicast with given title' do + expect(created_asciicast.title).to eq('bashing :)') + end + + it 'creates asciicast with given uname' do + expect(created_asciicast.uname).to eq('Linux 3.9.9-302.fc19.x86_64 #1 SMP Sat Jul 6 13:41:07 UTC 2013 x86_64') + end + + it 'creates asciicast with no user agent set' do + expect(created_asciicast.user_agent).to be(nil) + end + + context 'when a user with given token does not exist' do + let(:created_user) { User.last } + + it 'creates new user with given username and token' do + expect(created_user.temporary_username).to eq('kill') + expect(created_user.api_tokens.first.token).to eq('f33e6188-f53c-11e2-abf4-84a6c827e88b') + end + + it 'creates asciicast assigned to newly created user' do + expect(created_asciicast.user).to eq(created_user) + end + end + + context 'when a user with given token exists' do + let(:user) { User.create_with_token('f33e6188-f53c-11e2-abf4-84a6c827e88b', 'kill') } + + subject do + user + make_request + end + + it 'creates asciicast assigned to a user with given token' do + expect(created_asciicast.user).to eq(user) + end + end - it 'returns the URL to the uploaded asciicast' do - expect(response.body).to eq(asciicast_url(Asciicast.last)) + it 'returns the URL to the uploaded asciicast' do + expect(response.body).to eq(asciicast_url(created_asciicast)) + end end - context "when json includes uname (legacy)" do - let(:meta_filename) { 'meta-with-uname.json' } + context 'v0.9.8 client' do + subject { make_request } + + def make_request + post '/api/asciicasts', + { + asciicast: { + meta: fixture_file('0.9.8/meta.json', 'application/json'), + stdout: fixture_file('0.9.8/stdout', 'application/octet-stream'), + stdout_timing: fixture_file('0.9.8/stdout.time', 'application/octet-stream') + } + }, headers(nil, nil, 'asciinema/0.9.8 CPython/2.7.4 Jola/Misio-Foo') + end + + before { subject } + + it 'creates asciicast with given command' do + expect(created_asciicast.command).to eq('/bin/bash') + end + + it 'creates asciicast with given duration' do + expect(created_asciicast.duration).to eq(11.146430015564) + end + + it 'creates asciicast with given shell' do + expect(created_asciicast.shell).to eq('/bin/zsh') + end + + it 'creates asciicast with given terminal type' do + expect(created_asciicast.terminal_type).to eq('screen-256color') + end + + it 'creates asciicast with given terminal width' do + expect(created_asciicast.terminal_columns).to eq(96) + end + + it 'creates asciicast with given terminal height' do + expect(created_asciicast.terminal_lines).to eq(26) + end + + it 'creates asciicast with given title' do + expect(created_asciicast.title).to eq('bashing :)') + end + + it 'creates asciicast with given uname' do + expect(created_asciicast.uname).to be(nil) + end + + it 'creates asciicast with given user agent' do + expect(created_asciicast.user_agent).to eq('asciinema/0.9.8 CPython/2.7.4 Jola/Misio-Foo') + end + + context 'when a user with given token does not exist' do + let(:created_user) { User.last } + + it 'creates new user with given username and token' do + expect(created_user.temporary_username).to eq('kill') + expect(created_user.api_tokens.first.token).to eq('f33e6188-f53c-11e2-abf4-84a6c827e88b') + end + + it 'creates asciicast assigned to newly created user' do + expect(created_asciicast.user).to eq(created_user) + end + end + + context 'when a user with given token exists' do + let(:user) { User.create_with_token('f33e6188-f53c-11e2-abf4-84a6c827e88b', 'kill') } + + subject do + user + make_request + end + + it 'creates asciicast assigned to a user with given token' do + expect(created_asciicast.user).to eq(user) + end + end it 'returns the URL to the uploaded asciicast' do - expect(response.body).to eq(asciicast_url(Asciicast.last)) + expect(response.body).to eq(asciicast_url(created_asciicast)) end end - context "when json doesn't include user_token (anonymous?)" do - let(:meta_filename) { 'meta-no-token.json' } + context 'v0.9.9 client' do + subject { make_request } + + def make_request + post '/api/asciicasts', + { + asciicast: { + meta: fixture_file('0.9.9/meta.json', 'application/json'), + stdout: fixture_file('0.9.9/stdout', 'application/octet-stream'), + stdout_timing: fixture_file('0.9.9/stdout.time', 'application/octet-stream') + } + }, headers('kill', 'f33e6188-f53c-11e2-abf4-84a6c827e88b', 'asciinema/0.9.9 gc/go1.3 jola-amd64') + end + + before { subject } + + it 'creates asciicast with given command' do + expect(created_asciicast.command).to eq('/bin/bash') + end + + it 'creates asciicast with given duration' do + expect(created_asciicast.duration).to eq(11.146430015564) + end + + it 'creates asciicast with given shell' do + expect(created_asciicast.shell).to eq('/bin/zsh') + end + + it 'creates asciicast with given terminal type' do + expect(created_asciicast.terminal_type).to eq('screen-256color') + end + + it 'creates asciicast with given terminal width' do + expect(created_asciicast.terminal_columns).to eq(96) + end + + it 'creates asciicast with given terminal height' do + expect(created_asciicast.terminal_lines).to eq(26) + end + + it 'creates asciicast with given title' do + expect(created_asciicast.title).to eq('bashing :)') + end + + it 'creates asciicast with given uname' do + expect(created_asciicast.uname).to be(nil) + end + + it 'creates asciicast with given user agent' do + expect(created_asciicast.user_agent).to eq('asciinema/0.9.9 gc/go1.3 jola-amd64') + end + + context 'when a user with given token does not exist' do + let(:created_user) { User.last } + + it 'creates new user with given username and token' do + expect(created_user.temporary_username).to eq('kill') + expect(created_user.api_tokens.first.token).to eq('f33e6188-f53c-11e2-abf4-84a6c827e88b') + end + + it 'creates asciicast assigned to newly created user' do + expect(created_asciicast.user).to eq(created_user) + end + end + + context 'when a user with given token exists' do + let(:user) { User.create_with_token('f33e6188-f53c-11e2-abf4-84a6c827e88b', 'kill') } + + subject do + user + make_request + end + + it 'creates asciicast assigned to a user with given token' do + expect(created_asciicast.user).to eq(user) + end + end it 'returns the URL to the uploaded asciicast' do - expect(response.body).to eq(asciicast_url(Asciicast.last)) + expect(response.body).to eq(asciicast_url(created_asciicast)) end end diff --git a/spec/fixtures/0.9.7/meta.json b/spec/fixtures/0.9.7/meta.json new file mode 100644 index 0000000..f7773fc --- /dev/null +++ b/spec/fixtures/0.9.7/meta.json @@ -0,0 +1,15 @@ +{ + "command": "/bin/bash", + "duration": 11.146430015564, + "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", + "user_token": "f33e6188-f53c-11e2-abf4-84a6c827e88b", + "username": "kill" +} diff --git a/spec/fixtures/0.9.7/stdout b/spec/fixtures/0.9.7/stdout new file mode 100644 index 0000000..e6fab7a Binary files /dev/null and b/spec/fixtures/0.9.7/stdout differ diff --git a/spec/fixtures/0.9.7/stdout.time b/spec/fixtures/0.9.7/stdout.time new file mode 100644 index 0000000..e2882b8 Binary files /dev/null and b/spec/fixtures/0.9.7/stdout.time differ diff --git a/spec/fixtures/0.9.8/meta.json b/spec/fixtures/0.9.8/meta.json new file mode 100644 index 0000000..f4aef74 --- /dev/null +++ b/spec/fixtures/0.9.8/meta.json @@ -0,0 +1,14 @@ +{ + "command": "/bin/bash", + "duration": 11.146430015564, + "recorded_at": "Thu, 25 Jul 2013 20:08:57 +0000", + "shell": "/bin/zsh", + "term": { + "columns": 96, + "lines": 26, + "type": "screen-256color" + }, + "title": "bashing :)", + "user_token": "f33e6188-f53c-11e2-abf4-84a6c827e88b", + "username": "kill" +} diff --git a/spec/fixtures/0.9.8/stdout b/spec/fixtures/0.9.8/stdout new file mode 100644 index 0000000..e6fab7a Binary files /dev/null and b/spec/fixtures/0.9.8/stdout differ diff --git a/spec/fixtures/0.9.8/stdout.time b/spec/fixtures/0.9.8/stdout.time new file mode 100644 index 0000000..e2882b8 Binary files /dev/null and b/spec/fixtures/0.9.8/stdout.time differ diff --git a/spec/fixtures/0.9.9/meta.json b/spec/fixtures/0.9.9/meta.json new file mode 100644 index 0000000..79e3514 --- /dev/null +++ b/spec/fixtures/0.9.9/meta.json @@ -0,0 +1,11 @@ +{ + "command": "/bin/bash", + "duration": 11.146430015564, + "shell": "/bin/zsh", + "term": { + "columns": 96, + "lines": 26, + "type": "screen-256color" + }, + "title": "bashing :)" +} diff --git a/spec/fixtures/0.9.9/stdout b/spec/fixtures/0.9.9/stdout new file mode 100644 index 0000000..26374a6 Binary files /dev/null and b/spec/fixtures/0.9.9/stdout differ diff --git a/spec/fixtures/0.9.9/stdout.time b/spec/fixtures/0.9.9/stdout.time new file mode 100644 index 0000000..f9e426d Binary files /dev/null and b/spec/fixtures/0.9.9/stdout.time differ