Remove unused code

This commit is contained in:
Marcin Kulik 2017-06-24 22:44:11 +02:00
parent 6824a85cc3
commit e9f831ab93
5 changed files with 0 additions and 249 deletions

View File

@ -1,7 +1,5 @@
class ApiToken < ActiveRecord::Base class ApiToken < ActiveRecord::Base
ApiTokenTakenError = Class.new(StandardError)
belongs_to :user belongs_to :user
validates :user, :token, presence: true validates :user, :token, presence: true
@ -10,10 +8,6 @@ class ApiToken < ActiveRecord::Base
scope :active, -> { where(revoked_at: nil) } scope :active, -> { where(revoked_at: nil) }
scope :revoked, -> { where('revoked_at IS NOT NULL') } 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) def self.create_with_tmp_user!(token, username)
transaction do transaction do
ApiToken.create!( ApiToken.create!(
@ -23,13 +17,6 @@ class ApiToken < ActiveRecord::Base
end end
end end
def reassign_to(target_user)
return if target_user == user
raise ApiTokenTakenError if taken?
user.merge_to(target_user)
end
def revoke! def revoke!
update!(revoked_at: Time.now) update!(revoked_at: Time.now)
end end

View File

@ -36,15 +36,6 @@ class User < ActiveRecord::Base
with_username.where(username: username).first! with_username.where(username: username).first!
end 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) def self.for_auth_token(auth_token)
where(auth_token: auth_token).first where(auth_token: auth_token).first
end end
@ -88,26 +79,6 @@ class User < ActiveRecord::Base
theme_name && Theme.for_name(theme_name) theme_name && Theme.for_name(theme_name)
end 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 def public_asciicast_count
asciicasts.non_private.count asciicasts.non_private.count
end end

View File

@ -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

View File

@ -13,64 +13,4 @@ describe ApiToken do
it { should validate_uniqueness_of(:token) } it { should validate_uniqueness_of(:token) }
end 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 end

View File

@ -43,22 +43,6 @@ describe User do
end end
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 describe '.for_auth_token' do
subject { described_class.for_auth_token(auth_token) } subject { described_class.for_auth_token(auth_token) }
@ -106,36 +90,6 @@ describe User do
end end
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 describe '#asciicast_count' do
subject { user.asciicast_count } subject { user.asciicast_count }
@ -160,45 +114,4 @@ describe User do
end end
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 end