From 74337a56942d77db94273ddb62e65741006e3b31 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Tue, 20 Jun 2017 10:09:15 +0200 Subject: [PATCH] Remove old upload endpoint --- app/controllers/api/asciicasts_controller.rb | 26 -- app/controllers/api/base_controller.rb | 20 - app/strategies/api_token_strategy.rb | 22 - config/initializers/warden.rb | 2 - config/routes.rb | 4 - spec/api/asciicast_create_spec.rb | 438 ------------------- 6 files changed, 512 deletions(-) delete mode 100644 app/controllers/api/asciicasts_controller.rb delete mode 100644 app/controllers/api/base_controller.rb delete mode 100644 app/strategies/api_token_strategy.rb delete mode 100644 spec/api/asciicast_create_spec.rb diff --git a/app/controllers/api/asciicasts_controller.rb b/app/controllers/api/asciicasts_controller.rb deleted file mode 100644 index dc62865..0000000 --- a/app/controllers/api/asciicasts_controller.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Api - class AsciicastsController < BaseController - before_filter :ensure_authenticated! - - def create - asciicast = asciicast_creator.create(asciicast_attributes) - render text: asciicast_url(asciicast), status: :created, - location: asciicast - - rescue ActiveRecord::RecordInvalid => e - render text: e.record.errors.messages, status: 422 - rescue AsciicastParams::FormatError => e - render text: e.message, status: 400 - end - - private - - def asciicast_attributes - AsciicastParams.build(params[:asciicast], current_user, request.user_agent) - end - - def asciicast_creator - AsciicastCreator.new - end - end -end diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb deleted file mode 100644 index 4ae249e..0000000 --- a/app/controllers/api/base_controller.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'authentication/warden_authentication' - -module Api - class BaseController < ActionController::Base - - include WardenAuthentication - include RouteHelper - - private - - def warden_scope - :api - end - - def warden_strategies - [:api_token] - end - - end -end diff --git a/app/strategies/api_token_strategy.rb b/app/strategies/api_token_strategy.rb deleted file mode 100644 index 1f5328f..0000000 --- a/app/strategies/api_token_strategy.rb +++ /dev/null @@ -1,22 +0,0 @@ -class ApiTokenStrategy < ::Warden::Strategies::Base - - def valid? - auth.provided? && auth.basic? && auth.credentials - end - - def authenticate! - user = User.for_api_token(auth.credentials.last) - user.nil? ? fail!("Invalid auth token") : success!(user) - end - - def store? - false - end - - private - - def auth - @auth ||= Rack::Auth::Basic::Request.new(env) - end - -end diff --git a/config/initializers/warden.rb b/config/initializers/warden.rb index 4fe7c35..73629f2 100644 --- a/config/initializers/warden.rb +++ b/config/initializers/warden.rb @@ -7,7 +7,5 @@ Warden::Manager.serialize_from_session do |id| end require 'auth_cookie_strategy' -require 'api_token_strategy' Warden::Strategies.add(:auth_cookie, AuthCookieStrategy) -Warden::Strategies.add(:api_token, ApiTokenStrategy) diff --git a/config/routes.rb b/config/routes.rb index 11a9b43..d2197f5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,10 +18,6 @@ Rails.application.routes.draw do get "/oembed" => "oembed#show", as: :oembed - namespace :api do - resources :asciicasts, only: [:create] - end - resource :login, only: [:new, :create] do get :sent end diff --git a/spec/api/asciicast_create_spec.rb b/spec/api/asciicast_create_spec.rb deleted file mode 100644 index b26eab1..0000000 --- a/spec/api/asciicast_create_spec.rb +++ /dev/null @@ -1,438 +0,0 @@ -require 'rails_helper' - -describe "Asciicast creation" do - - 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) - {}.tap do |h| - h.merge!(basic_auth_header(user, password)) if user - h.merge!(user_agent_header(user_agent)) if user_agent - end - end - - 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 version 0' do - expect(created_asciicast.version).to eq(0) - end - - it 'creates asciicast with given stdout data file' do - expect(created_asciicast.stdout_data).to_not be(nil) - end - - it 'creates asciicast with given stdout timing file' do - expect(created_asciicast.stdout_timing).to_not be(nil) - end - - 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) { ApiToken.create_with_tmp_user!('f33e6188-f53c-11e2-abf4-84a6c827e88b', 'kill').user } - - 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(created_asciicast)) - end - end - - 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 version 0' do - expect(created_asciicast.version).to eq(0) - end - - it 'creates asciicast with given stdout data file' do - expect(created_asciicast.stdout_data).to_not be(nil) - end - - it 'creates asciicast with given stdout timing file' do - expect(created_asciicast.stdout_timing).to_not be(nil) - end - - 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 nil 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) { ApiToken.create_with_tmp_user!('f33e6188-f53c-11e2-abf4-84a6c827e88b', 'kill').user } - - 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(created_asciicast)) - end - end - - 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 version 0' do - expect(created_asciicast.version).to eq(0) - end - - it 'creates asciicast with given stdout data file' do - expect(created_asciicast.stdout_data).to_not be(nil) - end - - it 'creates asciicast with given stdout timing file' do - expect(created_asciicast.stdout_timing).to_not be(nil) - end - - 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 nil 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) { ApiToken.create_with_tmp_user!('f33e6188-f53c-11e2-abf4-84a6c827e88b', 'kill').user } - - 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(created_asciicast)) - end - end - - context 'format 1' do - subject { make_request } - - let(:username) { 'kill' } - let(:token) { 'f33e6188-f53c-11e2-abf4-84a6c827e88b' } - - def make_request(asciicast_path = '1/asciicast.json') - post '/api/asciicasts', - { asciicast: fixture_file(asciicast_path, 'application/json') }, - headers(username, token, 'asciinema/1.0.0 gc/go1.3 jola-amd64') - end - - before { subject } - - it 'creates asciicast version 1' do - expect(created_asciicast.version).to eq(1) - end - - it 'creates asciicast with given file' do - expect(created_asciicast.file).to_not be(nil) - end - - 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 nil 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/1.0.0 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) { ApiToken.create_with_tmp_user!('f33e6188-f53c-11e2-abf4-84a6c827e88b', 'kill').user } - - 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(created_asciicast)) - end - - context 'when json is missing data / has invalid data' do - subject { make_request('1/invalid.json') } - - it 'returns 422 status' do - expect(response.status).to eq(422) - end - end - - context 'when non-json file given' do - subject { make_request('stdout.decompressed') } - - it 'returns 400 status' do - expect(response.status).to eq(400) - end - end - - context 'when auth is not present' do - let(:username) { nil } - let(:token) { nil } - - it 'returns 401 status' do - expect(response.status).to eq(401) - end - end - - context 'when given token has been revoked' do - let(:token) { create(:revoked_api_token).token } - - it 'returns 401 status' do - expect(response.status).to eq(401) - end - end - - context 'when given token is invalid' do - let(:token) { 'foo' } - - it 'returns 401 status' do - expect(response.status).to eq(401) - end - end - - end - -end