Adding a bunch of tests

pull/69/head
Eugen Rochko 2016-02-26 15:28:08 +01:00
parent 44c5958203
commit f16b31f077
22 changed files with 439 additions and 105 deletions

View File

@ -33,6 +33,7 @@ group :development, :test do
gem 'pry-rails' gem 'pry-rails'
gem 'nyan-cat-formatter' gem 'nyan-cat-formatter'
gem 'fuubar' gem 'fuubar'
gem 'fabrication'
end end
group :test do group :test do

View File

@ -75,6 +75,7 @@ GEM
equalizer (0.0.11) equalizer (0.0.11)
erubis (2.7.0) erubis (2.7.0)
execjs (2.6.0) execjs (2.6.0)
fabrication (2.14.1)
fuubar (2.0.0) fuubar (2.0.0)
rspec (~> 3.0) rspec (~> 3.0)
ruby-progressbar (~> 1.4) ruby-progressbar (~> 1.4)
@ -302,6 +303,7 @@ DEPENDENCIES
binding_of_caller binding_of_caller
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)
dotenv-rails dotenv-rails
fabrication
fuubar fuubar
goldfinger goldfinger
grape grape

View File

@ -6,8 +6,8 @@ module ApplicationHelper
end end
def unique_tag_to_local_id(tag, expected_type) def unique_tag_to_local_id(tag, expected_type)
Regexp.new("objectId=([\d]+):objectType=#{expected_type}").match(tag) matches = Regexp.new("objectId=([\\d]+):objectType=#{expected_type}").match(tag)
return match[1] unless match.nil? return matches[1] unless matches.nil?
end end
def local_id?(id) def local_id?(id)

View File

@ -59,4 +59,12 @@ class Account < ActiveRecord::Base
def subscription(webhook_url) def subscription(webhook_url)
@subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url) @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
end end
before_create do
if local?
keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 48 : 2048)
self.private_key = keypair.to_pem
self.public_key = keypair.public_key.to_pem
end
end
end end

View File

@ -54,10 +54,9 @@ class Status < ActiveRecord::Base
unless reblog? unless reblog?
self.text.scan(Account::MENTION_RE).each do |match| self.text.scan(Account::MENTION_RE).each do |match|
uri = match.first uri = match.first
username = uri.split('@').first username, domain = uri.split('@')
domain = uri.split('@').size == 2 ? uri.split('@').last : nil account = Account.find_by(username: username, domain: domain)
account = Account.find_by(username: username, domain: domain)
m << account unless account.nil? m << account unless account.nil?
end end

View File

@ -29,7 +29,7 @@ class StreamEntry < ActiveRecord::Base
end end
def threaded? def threaded?
[:favorite, :comment].include? verb verb == :favorite || object_type == :comment
end end
def thread def thread

View File

@ -9,10 +9,6 @@ class SetupLocalAccountService < BaseService
user.account.username = username user.account.username = username
user.account.domain = nil user.account.domain = nil
keypair = OpenSSL::PKey::RSA.new(2048)
user.account.private_key = keypair.to_pem
user.account.public_key = keypair.public_key.to_pem
user.save! user.save!
end end
end end

View File

@ -0,0 +1,3 @@
Fabricator(:account) do
end

View File

@ -0,0 +1,3 @@
Fabricator(:favourite) do
end

View File

@ -0,0 +1,3 @@
Fabricator(:follow) do
end

View File

@ -0,0 +1,3 @@
Fabricator(:status) do
text "Lorem ipsum dolor sit amet"
end

View File

@ -0,0 +1,37 @@
require 'rails_helper'
RSpec.describe ApplicationHelper, type: :helper do
let(:local_domain) { 'local.tld' }
before do
stub_const('LOCAL_DOMAIN', local_domain)
end
describe '#unique_tag' do
it 'returns a string' do
expect(helper.unique_tag(Time.now, 12, 'Status')).to be_a String
end
end
describe '#unique_tag_to_local_id' do
it 'returns the ID part' do
expect(helper.unique_tag_to_local_id("tag:#{local_domain};objectId=12:objectType=Status", 'Status')).to eql '12'
end
end
describe '#local_id?' do
it 'returns true for a local ID' do
expect(helper.local_id?("tag:#{local_domain};objectId=12:objectType=Status")).to be true
end
it 'returns false for a foreign ID' do
expect(helper.local_id?('tag:foreign.tld;objectId=12:objectType=Status')).to be false
end
end
describe '#add_base_url_prefix' do
it 'returns full API URL from base to suffix' do
expect(helper.add_base_url_prefix('test')).to eql "#{root_url}api/test"
end
end
end

View File

@ -1,15 +1,115 @@
require 'rails_helper' require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the AtomHelper. For example:
#
# describe AtomHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe AtomHelper, type: :helper do RSpec.describe AtomHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}" describe '#stream_updated_at' do
pending
end
describe '#entry' do
pending
end
describe '#feed' do
pending
end
describe '#unique_id' do
pending
end
describe '#simple_id' do
pending
end
describe '#published_at' do
pending
end
describe '#updated_at' do
pending
end
describe '#verb' do
pending
end
describe '#content' do
pending
end
describe '#title' do
pending
end
describe '#author' do
pending
end
describe '#target' do
pending
end
describe '#object_type' do
pending
end
describe '#uri' do
pending
end
describe '#name' do
pending
end
describe '#summary' do
pending
end
describe '#subtitle' do
pending
end
describe '#link_alternate' do
pending
end
describe '#link_self' do
pending
end
describe '#link_hub' do
pending
end
describe '#link_salmon' do
pending
end
describe '#portable_contact' do
pending
end
describe '#in_reply_to' do
pending
end
describe '#link_mention' do
pending
end
describe '#disambiguate_uri' do
pending
end
describe '#disambiguate_url' do
pending
end
describe '#include_author' do
pending
end
describe '#include_entry' do
pending
end
end end

View File

@ -1,15 +1,5 @@
require 'rails_helper' require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the HomeHelper. For example:
#
# describe HomeHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe HomeHelper, type: :helper do RSpec.describe HomeHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end end

View File

@ -1,15 +1,5 @@
require 'rails_helper' require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the ProfileHelper. For example:
#
# describe ProfileHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe ProfileHelper, type: :helper do RSpec.describe ProfileHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end end

View File

@ -1,15 +1,5 @@
require 'rails_helper' require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the RoutingHelper. For example:
#
# describe RoutingHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe RoutingHelper, type: :helper do RSpec.describe RoutingHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end end

View File

@ -1,15 +1,5 @@
require 'rails_helper' require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the XrdHelper. For example:
#
# describe XrdHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe XrdHelper, type: :helper do RSpec.describe XrdHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end end

View File

@ -1,47 +1,110 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Account, type: :model do RSpec.describe Account, type: :model do
describe '#follow!' do subject { Fabricate(:account, username: 'alice') }
pending
end
describe '#unfollow!' do context do
pending let(:bob) { Fabricate(:account, username: 'bob') }
end
describe '#following?' do describe '#follow!' do
pending it 'creates a follow' do
follow = subject.follow!(bob)
expect(follow).to be_instance_of Follow
expect(follow.account).to eq subject
expect(follow.target_account).to eq bob
end
end
describe '#unfollow!' do
before do
subject.follow!(bob)
end
it 'destroys a follow' do
unfollow = subject.unfollow!(bob)
expect(unfollow).to be_instance_of Follow
expect(unfollow.account).to eq subject
expect(unfollow.target_account).to eq bob
expect(unfollow.destroyed?).to be true
end
end
describe '#following?' do
it 'returns true when the target is followed' do
subject.follow!(bob)
expect(subject.following?(bob)).to be true
end
it 'returns false if the target is not followed' do
expect(subject.following?(bob)).to be false
end
end
end end
describe '#local?' do describe '#local?' do
pending it 'returns true when the account is local' do
expect(subject.local?).to be true
end
it 'returns false when the account is on a different domain' do
subject.domain = 'foreign.tld'
expect(subject.local?).to be false
end
end end
describe '#acct' do describe '#acct' do
pending it 'returns username for local users' do
expect(subject.acct).to eql 'alice'
end
it 'returns username@domain for foreign users' do
subject.domain = 'foreign.tld'
expect(subject.acct).to eql 'alice@foreign.tld'
end
end end
describe '#subscribed?' do describe '#subscribed?' do
pending it 'returns false when no secrets and tokens have been set' do
expect(subject.subscribed?).to be false
end
it 'returns true when the secret and token have been set' do
subject.secret = 'a'
subject.verify_token = 'b'
expect(subject.subscribed?).to be true
end
end end
describe '#keypair' do describe '#keypair' do
pending it 'returns an RSA key pair' do
expect(subject.keypair).to be_instance_of OpenSSL::PKey::RSA
end
end end
describe '#subscription' do describe '#subscription' do
pending it 'returns an OStatus subscription' do
expect(subject.subscription('')).to be_instance_of OStatus2::Subscription
end
end end
describe '#object_type' do describe '#object_type' do
pending it 'is always a person' do
expect(subject.object_type).to be :person
end
end end
describe '#title' do describe '#title' do
pending it 'is the same as the username' do
expect(subject.title).to eql subject.username
end
end end
describe '#content' do describe '#content' do
pending it 'is the same as the note' do
expect(subject.content).to eql subject.note
end
end end
end end

View File

@ -1,31 +1,56 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Favourite, type: :model do RSpec.describe Favourite, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:status) { Fabricate(:status, account: bob) }
subject { Favourite.new(account: alice, status: status) }
describe '#verb' do describe '#verb' do
pending it 'is always favorite' do
expect(subject.verb).to be :favorite
end
end end
describe '#title' do describe '#title' do
pending it 'describes the favourite' do
expect(subject.title).to eql 'alice favourited a status by bob'
end
end end
describe '#content' do describe '#content' do
pending it 'equals the title' do
expect(subject.content).to eq subject.title
end
end end
describe '#object_type' do describe '#object_type' do
pending it 'is a note when the target is a note' do
expect(subject.object_type).to be :note
end
it 'is a comment when the target is a comment' do
status.in_reply_to_id = 2
expect(subject.object_type).to be :comment
end
end end
describe '#target' do describe '#target' do
pending it 'is the status that was favourited' do
expect(subject.target).to eq status
end
end end
describe '#mentions' do describe '#mentions' do
pending it 'is always empty' do
expect(subject.mentions).to be_empty
end
end end
describe '#thread' do describe '#thread' do
pending it 'equals the target' do
expect(subject.thread).to eq subject.target
end
end end
end end

View File

@ -1,27 +1,44 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Follow, type: :model do RSpec.describe Follow, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
let(:bob) { Fabricate(:account, username: 'bob') }
subject { Follow.new(account: alice, target_account: bob) }
describe '#verb' do describe '#verb' do
pending it 'is follow' do
expect(subject.verb).to be :follow
end
end end
describe '#title' do describe '#title' do
pending it 'describes the follow' do
expect(subject.title).to eql 'alice started following bob'
end
end end
describe '#content' do describe '#content' do
pending it 'is the same as the title' do
expect(subject.content).to eql subject.title
end
end end
describe '#object_type' do describe '#object_type' do
pending it 'is a person' do
expect(subject.object_type).to be :person
end
end end
describe '#target' do describe '#target' do
pending it 'is the person being followed' do
expect(subject.target).to eq bob
end
end end
describe '#mentions' do describe '#mentions' do
pending it 'is empty' do
expect(subject.mentions).to be_empty
end
end end
end end

View File

@ -1,35 +1,117 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Status, type: :model do RSpec.describe Status, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:other) { Fabricate(:status, account: bob, text: 'Skulls for the skull god! The enemy\'s gates are sideways!')}
subject { Fabricate(:status, account: alice) }
describe '#local?' do describe '#local?' do
pending it 'returns true when no remote URI is set' do
expect(subject.local?).to be true
end
it 'returns false if a remote URI is set' do
subject.uri = 'a'
expect(subject.local?).to be false
end
end end
describe '#reblog?' do describe '#reblog?' do
pending it 'returns true when the status reblogs another status' do
subject.reblog = other
expect(subject.reblog?).to be true
end
it 'returns false if the status is self-contained' do
expect(subject.reblog?).to be false
end
end end
describe '#reply?' do describe '#reply?' do
pending it 'returns true if the status references another' do
subject.thread = other
expect(subject.reply?).to be true
end
it 'returns false if the status is self-contained' do
expect(subject.reply?).to be false
end
end end
describe '#mentions' do describe '#mentions' do
pending before do
bob # make sure the account exists
end
it 'is empty if the status is self-contained and does not mention anyone' do
expect(subject.mentions).to be_empty
end
it 'returns mentioned accounts' do
subject.text = 'Hello @bob!'
expect(subject.mentions).to include bob
end
it 'returns account of the replied-to status' do
subject.thread = other
expect(subject.mentions).to include bob
end
it 'returns the account of the shared status' do
subject.reblog = other
expect(subject.mentions).to include bob
end
end end
describe '#verb' do describe '#verb' do
pending it 'is always post' do
expect(subject.verb).to be :post
end
end end
describe '#object_type' do describe '#object_type' do
pending it 'is note when the status is self-contained' do
expect(subject.object_type).to be :note
end
it 'is comment when the status replies to another' do
subject.thread = other
expect(subject.object_type).to be :comment
end
end end
describe '#title' do describe '#title' do
pending it 'is a shorter version of the content' do
expect(subject.title).to be_a String
end
end
describe '#content' do
it 'returns the text of the status if it is not a reblog' do
expect(subject.content).to eql subject.text
end
it 'returns the text of the reblogged status' do
subject.reblog = other
expect(subject.content).to eql other.text
end
end end
describe '#target' do describe '#target' do
pending it 'returns nil if the status is self-contained' do
expect(subject.target).to be_nil
end
it 'returns nil if the status is a reply' do
subject.thread = other
expect(subject.target).to be_nil
end
it 'returns the reblogged status' do
subject.reblog = other
expect(subject.target).to eq other
end
end end
end end

View File

@ -1,11 +1,43 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe StreamEntry, type: :model do RSpec.describe StreamEntry, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:follow) { Fabricate(:follow, account: alice, target_account: bob) }
let(:status) { Fabricate(:status, account: alice) }
let(:reblog) { Fabricate(:status, account: bob, reblog: status) }
let(:reply) { Fabricate(:status, account: bob, thread: status) }
let(:favourite) { Fabricate(:favourite, account: alice, status: status) }
describe '#targeted?' do describe '#targeted?' do
pending it 'returns true for a follow' do
expect(follow.stream_entry.targeted?).to be true
end
it 'returns true for a favourite' do
expect(favourite.stream_entry.targeted?).to be true
end
it 'returns true for a reblog' do
expect(reblog.stream_entry.targeted?).to be true
end
it 'returns false otherwise' do
expect(status.stream_entry.targeted?).to be false
end
end end
describe '#threaded?' do describe '#threaded?' do
pending it 'returns true for a favourite' do
expect(favourite.stream_entry.threaded?).to be true
end
it 'returns true for a reply' do
expect(reply.stream_entry.threaded?).to be true
end
it 'returns false otherwise' do
expect(status.stream_entry.threaded?).to be false
end
end end
end end