Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scopes #154

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/challenge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Challenge < ApplicationRecord

before_create { |challenge| challenge.flag = BCrypt::Password.create(flag) }

default_scope { order(title: :asc) }
scope :active, -> { where(active: true) }

class String < SimpleDelegator
Expand Down
2 changes: 2 additions & 0 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ class Submission < ApplicationRecord

validates :submission_hash, presence: true, uniqueness: true
validates :user, :team, :challenge, :category, presence: true

scope :valid, -> { where(valid_submission: true) }
end
4 changes: 4 additions & 0 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ class Team < ApplicationRecord
uniqueness: { case_sensitive: false }

include Tokenable

def solved_challenges
submissions.valid
end
end
4 changes: 2 additions & 2 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
end

factory :category do
name { 'web' }
sequence(:name) { |n| "category-#{n}" }
end

factory :challenge do
Expand All @@ -53,7 +53,7 @@

factory :submission do
challenge
category
category { challenge.category }
flag { 'flag{invalid}' }
user
team
Expand Down
9 changes: 9 additions & 0 deletions spec/models/challenge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@
expect(@challenge.description.to_md).to eq("<h1>Finish the challenge to get the flag</h1>\n")
end
end

context 'when calling for challenges' do
let(:challenge_second) { FactoryBot.create(:challenge, title: 'challenge2', flag: 'flag{flag}') }
let(:challenge_first) { FactoryBot.create(:challenge, title: 'challenge1', flag: 'flag{flag}') }

it 'returns them sorted by title' do
expect(Challenge.all).to eq([challenge_first, challenge_second, @challenge])
end
end
end
7 changes: 7 additions & 0 deletions spec/models/submission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,11 @@
).to_not be_valid
end
end

context'when calling #valid' do
let(:valid_submission) { FactoryBot.create(:submission, valid_submission: true, flag: nil) }
it 'returns valid submissions' do
expect(Submission.valid).to eq([valid_submission])
end
end
end
9 changes: 9 additions & 0 deletions spec/models/team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
end
end

context 'when calling #solved_challenges' do
let(:team) { FactoryBot.create(:team) }
let(:submission) { FactoryBot.create(:submission, valid_submission: true, flag: nil, team: team) }

it 'returns a list of valid submissions' do
expect(team.solved_challenges).to eq([submission])
end
end

private

def create_users
Expand Down