Skip to content

Commit b2f00f0

Browse files
committed
fix all broken badge specs
1 parent 776a477 commit b2f00f0

13 files changed

+89
-108
lines changed

app/models/badges/early_adopter.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ class EarlyAdopter < BadgeBase
1010
FOUNDING_DATE = Date.parse('Oct 19, 2007')
1111

1212
def reasons
13-
found = user.facts.detect do |fact|
14-
fact.tagged?('github', 'account-created')
15-
end
16-
if found && found.relevant_on <= FOUNDING_DATE + 6.months
17-
"Created an account within GitHub's first 6 months on #{found.relevant_on.to_date.to_s(:long).to_s.capitalize}."
13+
if user.github_profile && user.github_profile.github_created_at <= FOUNDING_DATE + 6.months
14+
"Created an account within GitHub's first 6 months on #{user.github_profile.github_created_at.to_date.to_s(:long).to_s.capitalize}."
1815
else
1916
nil
2017
end
@@ -23,4 +20,4 @@ def reasons
2320
def award?
2421
!reasons.blank?
2522
end
26-
end
23+
end

app/models/users/github/profile.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ module Users
2323
module Github
2424
class Profile < ActiveRecord::Base
2525
belongs_to :user
26-
has_many :followers, class_name: 'Users::Github::Profiles::Follower' , foreign_key: :follower_id , dependent: :delete_all
27-
has_many :repositories, :class_name => 'Users::Github::Repository' , foreign_key: :owner_id
26+
has_many :followers, class_name: 'Users::Github::Profiles::Follower',
27+
foreign_key: :follower_id , dependent: :delete_all
28+
has_many :repositories, class_name: 'Users::Github::Repository',
29+
foreign_key: :owner_id
2830
validates :login , presence: true, uniqueness: true
2931
before_validation :copy_login_from_user, on: :create
3032
after_create :extract_data_from_github
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Fabricator(:github_repository, from: 'users/github/repository') do
2+
github_id 123456789
23
end

spec/models/badges/altruist_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
expect(Altruist.description).to include('20')
77
end
88

9-
it 'should award user if they have 50 or more original repos with contents' do
9+
it 'should award user if they have 20 or more original repos with contents' do
1010
user = Fabricate(:user, github: 'mdeiters')
1111

1212
20.times do

spec/models/badges/ashcat_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
VCR.configure do |c|
44
c.default_cassette_options = {
5-
:match_requests_on => [:method,
6-
VCR.request_matchers.uri_without_param(:client_id, :client_secret)]
5+
match_requests_on:
6+
[ :method,
7+
VCR.request_matchers.uri_without_param(:client_id, :client_secret)]
78
}
89
end
910

1011
RSpec.describe Ashcat, type: :model do
11-
let(:profile) { Fabricate(:github_profile) }
12-
let(:contributor) { Fabricate(:user, github_id: profile.github_id, github: 'dhh') }
12+
let(:contributor) { Fabricate(:user, github: 'dhh') }
1313

1414
it 'creates facts for each contributor' do
1515
# TODO: Refactor to utilize sidekiq job
1616
VCR.use_cassette('Ashcat') do
1717
Ashcat.perform
1818

19-
contributor.build_github_facts
20-
2119
badge = Ashcat.new(contributor)
2220
expect(badge.award?).to eq(true)
2321
expect(badge.reasons).to match(/Contributed \d+ times to Rails Core/)

spec/models/badges/badge_base_spec.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
require 'spec_helper'
22

33
RSpec.describe BadgeBase, type: :model do
4-
let(:repo) { Fabricate(:github_repo) }
5-
let(:profile) { Fabricate(:github_profile, github_id: repo.owner.github_id) }
6-
let(:user) { Fabricate(:user, github_id: profile.github_id) }
4+
let(:user) { Fabricate(:user, github: 'codebender') }
75

86
it 'should check to see if it needs to award users' do
9-
stub_request(:get, 'http://octocoder.heroku.com/rails/rails/mdeiters').to_return(body: '{}')
7+
stub_request(:get, 'http://octocoder.heroku.com/rails/rails/mdeiters').
8+
to_return(body: '{}')
109
allow(Octopussy).to receive(:new) do |*_args|
1110
octopussy_mock = double('Octopussy')
1211
expect(octopussy_mock).to receive(:valid?).and_return(true)
@@ -34,13 +33,4 @@
3433
expect(bar.image_name).to eq('bar.png')
3534
end
3635

37-
class NotaBadge < BadgeBase
38-
def award?
39-
true
40-
end
41-
42-
def reasons
43-
["I don't need a reason"]
44-
end
45-
end
4636
end

spec/models/badges/cub_spec.rb

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,42 @@
11
require 'spec_helper'
22

33
RSpec.describe Cub, type: :model do
4-
let(:languages) do {
5-
'JavaScript' => 111_435
6-
} end
7-
let(:repo) { Fabricate(:github_repo, languages: languages) }
8-
let(:profile) { Fabricate(:github_profile, github_id: repo.owner.github_id) }
9-
let(:user) { Fabricate(:user, github_id: profile.github_id) }
4+
let(:user) { Fabricate(:user, github: 'codebender') }
105

116
it 'should have a name and description' do
127
expect(Cub.description).not_to be_nil
138
end
149

1510
it 'should award the user if they have a repo tagged with JQuery' do
16-
repo.add_tag('JQuery')
17-
repo.save!
18-
19-
user.build_github_facts
11+
fact = Fabricate(:github_original_fact, context: user,
12+
tags: %w(JQuery repo original personal github))
2013

2114
badge = Cub.new(user)
2215
expect(badge.award?).to eq(true)
2316
expect(badge.reasons[:links]).not_to be_empty
2417
end
2518

26-
it 'should not award if repo when readme contains text and is less then 90 javascript' do
27-
languages['JavaScript'] = 230_486
28-
languages['Ruby'] = 20_364
29-
30-
user.build_github_facts
19+
it 'should not award if javascript is not the dominent language' do
20+
fact = Fabricate(:github_original_fact, context: user,
21+
tags: %w(Objective-C repo original personal github))
3122

3223
badge = Cub.new(user)
3324
expect(badge.award?).to eq(false)
3425
end
3526

3627
it 'should award the user if they have a repo tagged with Prototype' do
37-
repo.add_tag('Prototype')
38-
repo.save!
39-
40-
user.build_github_facts
28+
fact = Fabricate(:github_original_fact, context: user,
29+
tags: %w(Prototype repo original personal github))
4130

4231
badge = Cub.new(user)
4332
expect(badge.award?).to eq(true)
4433
end
4534

4635
it 'should not support forks' do
47-
repo.fork = true
48-
repo.save!
36+
fact = Fabricate(:github_original_fact, context: user,
37+
tags: %w(Prototype repo fork personal github))
38+
fact = Fabricate(:github_original_fact, context: user,
39+
tags: %w(JQuery repo fork personal github))
4940

5041
user.build_github_facts
5142

spec/models/badges/early_adopter_spec.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
require 'spec_helper'
22

33
RSpec.describe EarlyAdopter, type: :model do
4+
let(:user) { Fabricate(:user, github: 'codebender') }
5+
6+
before(:each) do
7+
allow(ExtractGithubProfile).to receive(:perform_async)
8+
end
9+
410
it 'should have a name and description' do
511
expect(EarlyAdopter.name).not_to be_blank
612
expect(EarlyAdopter.description).not_to be_blank
713
end
814

915
it 'should award user if they joined github within 6 months of founding' do
10-
profile = Fabricate(:github_profile, created_at: '2008/04/14 15:53:10 -0700')
11-
user = Fabricate(:user, github_id: profile.github_id)
12-
13-
user.build_github_facts
16+
profile = Fabricate(:github_profile, user: user,
17+
github_created_at: '2008/04/14 15:53:10 -0700')
1418

1519
badge = EarlyAdopter.new(user)
1620
expect(badge.award?).to eq(true)
1721
expect(badge.reasons).to eq("Created an account within GitHub's first 6 months on April 14, 2008.")
1822
end
1923

2024
it 'should not award the user if the they joined after 6 mounts of github founding' do
21-
profile = Fabricate(:github_profile, created_at: '2009/04/14 15:53:10 -0700')
22-
user = Fabricate(:user, github_id: profile.github_id)
25+
profile = Fabricate(:github_profile, user: user,
26+
github_created_at: '2009/04/14 15:53:10 -0700')
2327

24-
user.build_github_facts
28+
badge = EarlyAdopter.new(user)
29+
expect(badge.award?).to eq(false)
30+
end
2531

32+
it 'does not award the badge if the user doesnt have a github profile' do
2633
badge = EarlyAdopter.new(user)
2734
expect(badge.award?).to eq(false)
2835
end

spec/models/badges/mongoose_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
require 'spec_helper'
22

33
RSpec.describe Mongoose, type: :model do
4-
let(:languages) do {
5-
'Ruby' => 2_519_686,
6-
'JavaScript' => 6107,
7-
'Python' => 76_867
8-
} end
9-
let(:repo) { Fabricate(:github_repo, languages: languages) }
10-
let(:profile) { Fabricate(:github_profile, github_id: repo.owner.github_id) }
11-
let(:user) { Fabricate(:user, github_id: profile.github_id) }
4+
let(:user) { Fabricate(:user, github: 'codebender') }
125

136
before :all do
147
Fact.delete_all
@@ -19,16 +12,25 @@
1912
end
2013

2114
it 'should award ruby dev with one ruby repo' do
22-
user.build_github_facts
15+
fact = Fabricate(:github_original_fact, context: user,
16+
tags: %w(Ruby repo original personal github))
2317

2418
badge = Mongoose.new(user)
2519
expect(badge.award?).to eq(true)
2620
expect(badge.reasons[:links]).not_to be_empty
2721
end
2822

29-
it 'should not for a python dev' do
30-
languages.delete('Ruby')
31-
user.build_github_facts
23+
it 'should not for a dev with no repo with ruby as the dominent language' do
24+
fact = Fabricate(:github_original_fact, context: user,
25+
tags: %w(Python repo original personal github))
26+
27+
badge = Mongoose.new(user)
28+
expect(badge.award?).to eq(false)
29+
end
30+
31+
it 'doesnt award the badge if the repo is a fork' do
32+
fact = Fabricate(:github_original_fact, context: user,
33+
tags: %w(Ruby repo fork personal github))
3234

3335
badge = Mongoose.new(user)
3436
expect(badge.award?).to eq(false)

spec/models/badges/nephila_komaci_spec.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
require 'spec_helper'
22

33
RSpec.describe NephilaKomaci, type: :model do
4-
let(:languages) do {
5-
'PHP' => 2_519_686,
6-
'Python' => 76_867
7-
} end
8-
let(:repo) { Fabricate(:github_repo, languages: languages) }
9-
let(:profile) { Fabricate(:github_profile, github_id: repo.owner.github_id) }
10-
let(:user) { Fabricate(:user, github_id: profile.github_id) }
4+
let(:user) { Fabricate(:user, github: 'codebender') }
115

126
before :all do
137
Fact.delete_all
@@ -17,8 +11,9 @@
1711
expect(NephilaKomaci.description).not_to be_blank
1812
end
1913

20-
it 'should award php dev with badge' do
21-
user.build_github_facts
14+
it 'should award the badge if the user has a original PHP dominent repo' do
15+
fact = Fabricate(:github_original_fact, context: user,
16+
tags: %w(PHP repo original personal github))
2217

2318
badge = NephilaKomaci.new(user)
2419
expect(badge.award?).to eq(true)

spec/models/badges/octopussy_spec.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
require 'spec_helper'
22

33
RSpec.describe Octopussy, type: :model do
4-
let(:repo) { Fabricate(:github_repo) }
5-
let(:profile) { Fabricate(:github_profile, github_id: repo.owner.github_id) }
6-
let(:user) { Fabricate(:user, github_id: profile.github_id) }
4+
let(:user) { Fabricate(:user, github: 'codebender') }
75
let(:pjhyett) { Fabricate(:user, github: 'pjhyett') }
86

97
it 'should have a name and description' do
@@ -15,9 +13,9 @@
1513
create_team_github = Fabricate(:team, name: "Github")
1614
create_team_github.add_member(pjhyett)
1715

18-
random_dude = repo.followers.create! login: 'jmcneese'
19-
20-
user.build_github_facts
16+
fact = Fabricate(:github_original_fact, context: user,
17+
tags: %w(Ruby repo original personal github),
18+
metadata: { watchers: 'rubysolos' })
2119

2220
badge = Octopussy.new(user)
2321
expect(badge.award?).to eq(false)
@@ -27,16 +25,27 @@
2725
create_team_github = Fabricate(:team, name: "Github")
2826
create_team_github.add_member(pjhyett)
2927

30-
github_founder = repo.followers.create! login: 'pjhyett'
31-
repo.save!
32-
33-
user.build_github_facts
28+
fact = Fabricate(:github_original_fact, context: user,
29+
tags: %w(Ruby repo original personal github),
30+
metadata: { watchers: 'pjhyett' })
3431

3532
badge = Octopussy.new(user)
3633
expect(badge.award?).to eq(true)
3734
expect(badge.reasons[:links]).not_to be_empty
3835
end
3936

37+
it 'does not award forked repos' do
38+
create_team_github = Fabricate(:team, name: "Github")
39+
create_team_github.add_member(pjhyett)
40+
41+
fact = Fabricate(:github_original_fact, context: user,
42+
tags: %w(Ruby repo fork personal github),
43+
metadata: { watchers: 'pjhyett' })
44+
45+
badge = Octopussy.new(user)
46+
expect(badge.award?).to eq(false)
47+
end
48+
4049
it 'should cache github team members' do
4150
create_team_github = Fabricate(:team, name: "Github")
4251
create_team_github.add_member(pjhyett)

spec/models/badges/python_spec.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
require 'spec_helper'
22

33
RSpec.describe Python, type: :model do
4-
let(:languages) do {
5-
'Python' => 2_519_686,
6-
'Java' => 76_867
7-
} end
8-
let(:repo) { Fabricate(:github_repo, languages: languages) }
9-
let(:profile) { Fabricate(:github_profile, github_id: repo.owner.github_id) }
10-
let(:user) { Fabricate(:user, github_id: profile.github_id) }
4+
let(:user) { Fabricate(:user, github: 'codebender') }
115

126
it 'should have a name and description' do
137
expect(Python.description).not_to be_blank
148
end
159

16-
it 'should not award ruby dev with one ruby repo' do
17-
user.build_github_facts
10+
it 'awards the user if the user has a Python dominent repo' do
11+
fact = Fabricate(:github_original_fact, context: user,
12+
tags: %w(Python repo original personal github))
1813

1914
badge = Python.new(user)
2015
expect(badge.award?).to eq(true)
2116
expect(badge.reasons[:links]).not_to be_empty
2217
end
2318

24-
it 'should not for a python dev' do
25-
languages.delete('Python')
26-
user.build_github_facts
19+
it 'does not award the user if the user has no Python dominent repo' do
20+
fact = Fabricate(:github_original_fact, context: user,
21+
tags: %w(Ruby repo original personal github))
2722

2823
badge = Python.new(user)
2924
expect(badge.award?).to eq(false)

0 commit comments

Comments
 (0)