diff --git a/app/models/api_token.rb b/app/models/api_token.rb index ce07dcc..65ed089 100644 --- a/app/models/api_token.rb +++ b/app/models/api_token.rb @@ -1,7 +1,5 @@ class ApiToken < ActiveRecord::Base - ApiTokenTakenError = Class.new(StandardError) - belongs_to :user validates :user, :token, presence: true @@ -10,10 +8,6 @@ class ApiToken < ActiveRecord::Base scope :active, -> { where(revoked_at: nil) } scope :revoked, -> { where('revoked_at IS NOT NULL') } - def self.for_token(token) - where(token: token).first - end - def self.create_with_tmp_user!(token, username) transaction do ApiToken.create!( @@ -23,13 +17,6 @@ class ApiToken < ActiveRecord::Base end end - def reassign_to(target_user) - return if target_user == user - raise ApiTokenTakenError if taken? - - user.merge_to(target_user) - end - def revoke! update!(revoked_at: Time.now) end diff --git a/app/models/user.rb b/app/models/user.rb index af8628a..ad8018e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -36,15 +36,6 @@ class User < ActiveRecord::Base with_username.where(username: username).first! end - def self.for_api_token(token) - return nil if token.blank? - - joins(:api_tokens).where( - 'api_tokens.token' => token, - 'api_tokens.revoked_at' => nil, - ).first - end - def self.for_auth_token(auth_token) where(auth_token: auth_token).first end @@ -88,26 +79,6 @@ class User < ActiveRecord::Base theme_name && Theme.for_name(theme_name) end - def assign_api_token(token) - api_token = ApiToken.for_token(token) - - if api_token - api_token.reassign_to(self) - else - api_token = api_tokens.create!(token: token) - end - - api_token - end - - def merge_to(target_user) - self.class.transaction do |tx| - asciicasts.update_all(user_id: target_user.id, updated_at: DateTime.now) - api_tokens.update_all(user_id: target_user.id, updated_at: DateTime.now) - destroy - end - end - def public_asciicast_count asciicasts.non_private.count end diff --git a/spec/controllers/api_tokens_controller_spec.rb b/spec/controllers/api_tokens_controller_spec.rb deleted file mode 100644 index d8c4bf2..0000000 --- a/spec/controllers/api_tokens_controller_spec.rb +++ /dev/null @@ -1,60 +0,0 @@ -require 'rails_helper' - -describe ApiTokensController do - - describe '#create' do - subject { get :create, api_token: 'a-toh-can' } - - let(:user) { double('user', assign_api_token: nil, username: 'foobar') } - - before do - login_as(user) - end - - context 'for guest user' do - let(:user) { nil } - - before do - subject - end - - it { should redirect_to(new_login_path) } - - specify { expect(flash[:notice]).to match(/log in to proceed/) } - end - - context "when assigning succeeds" do - before do - allow(user).to receive(:assign_api_token).with('a-toh-can') - subject - end - - it { should redirect_to(public_profile_path(username: 'foobar')) } - - specify { expect(flash[:notice]).to_not be_blank } - end - - context "when token is invalid" do - before do - allow(user).to receive(:assign_api_token).with('a-toh-can'). - and_raise(ActiveRecord::RecordInvalid, ApiToken.new) - end - - it 'displays error page' do - expect(subject).to render_template(:error) - end - end - - context "when token is taken" do - before do - allow(user).to receive(:assign_api_token).with('a-toh-can'). - and_raise(ApiToken::ApiTokenTakenError) - end - - it 'displays error page' do - expect(subject).to render_template(:error) - end - end - end - -end diff --git a/spec/models/api_token_spec.rb b/spec/models/api_token_spec.rb index 59c4906..9d350b9 100644 --- a/spec/models/api_token_spec.rb +++ b/spec/models/api_token_spec.rb @@ -13,64 +13,4 @@ describe ApiToken do it { should validate_uniqueness_of(:token) } end - describe '.for_token' do - subject { described_class.for_token(token) } - - context "when ApiToken with given token exists" do - let(:token) { attributes_for(:api_token)[:token] } - let!(:api_token) { create(:api_token, token: token) } - - it { should eq(api_token) } - end - - context "when ApiToken with given token doesn't exist" do - let(:token) { 'no-no' } - - it { should be(nil) } - end - end - - describe '#reassign_to' do - subject { api_token.reassign_to(target_user) } - - let(:api_token) { described_class.new } - let(:user) { User.new } - let(:target_user) { User.new } - - before do - api_token.user = user - allow(user).to receive(:merge_to) - end - - context "when source user is unconfirmed user" do - before do - allow(user).to receive(:confirmed?) { false } - end - - it "merges user to target user" do - subject - expect(user).to have_received(:merge_to).with(target_user) - end - end - - context "when target user is the same user" do - let(:target_user) { user } - - it "doesn't do anything" do - subject - expect(user).to_not have_received(:merge_to) - end - end - - context "when source user is confirmed user" do - before do - allow(user).to receive(:confirmed?) { true } - end - - it "raises ApiTokenTakenError" do - expect { subject }.to raise_error(ApiToken::ApiTokenTakenError) - end - end - end - end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 35b4233..405ba83 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -43,22 +43,6 @@ describe User do end end - describe '.for_api_token' do - subject { described_class.for_api_token(token) } - - let(:token) { 'f33e6188-f53c-11e2-abf4-84a6c827e88b' } - - context "when token exists" do - let!(:existing_token) { create(:api_token, token: token) } - - it { should eq(existing_token.user) } - end - - context "when token doesn't exist" do - it { should be(nil) } - end - end - describe '.for_auth_token' do subject { described_class.for_auth_token(auth_token) } @@ -106,36 +90,6 @@ describe User do end end - describe '#assign_api_token' do - subject { user.assign_api_token(token) } - - let(:user) { create(:user) } - let(:token) { 'a33e6188-f53c-11e2-abf4-84a6c827e88b' } - - before do - allow(ApiToken).to receive(:for_token).with(token) { api_token } - end - - context "when given token doesn't exist" do - let(:api_token) { nil } - - it { should be_kind_of(ApiToken) } - it { should be_persisted } - specify { expect(subject.token).to eq(token) } - end - - context "when given token already exists" do - let(:api_token) { double('api_token', reassign_to: nil) } - - it "reassigns it to the user" do - subject - expect(api_token).to have_received(:reassign_to).with(user) - end - - it { should be(api_token) } - end - end - describe '#asciicast_count' do subject { user.asciicast_count } @@ -160,45 +114,4 @@ describe User do end end - describe '#merge_to' do - subject { user.merge_to(target_user) } - - let(:user) { create(:user) } - let(:target_user) { create(:user) } - let!(:api_token_1) { create(:api_token, user: user) } - let!(:api_token_2) { create(:api_token, user: user) } - let!(:asciicast_1) { create(:asciicast, user: user) } - let!(:asciicast_2) { create(:asciicast, user: user) } - let(:updated_at) { 1.hour.from_now } - - before do - Timecop.freeze(updated_at) do - subject - end - end - - it "reassigns all user api tokens to the target user" do - api_token_1.reload - api_token_2.reload - - expect(api_token_1.user).to eq(target_user) - expect(api_token_2.user).to eq(target_user) - expect(api_token_1.updated_at.to_i).to eq(updated_at.to_i) - expect(api_token_2.updated_at.to_i).to eq(updated_at.to_i) - end - - it "reassigns all user asciicasts to the target user" do - asciicast_1.reload - asciicast_2.reload - - expect(asciicast_1.user).to eq(target_user) - expect(asciicast_2.user).to eq(target_user) - expect(asciicast_1.updated_at.to_i).to eq(updated_at.to_i) - expect(asciicast_2.updated_at.to_i).to eq(updated_at.to_i) - end - - it "removes the source user" do - expect(user).to be_destroyed - end - end end