diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 6567dfa6..aaea5d27 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -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 diff --git a/app/models/submission.rb b/app/models/submission.rb index a41047b3..58af6d1b 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -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 diff --git a/app/models/team.rb b/app/models/team.rb index 54906dd7..5de0ed35 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -9,4 +9,8 @@ class Team < ApplicationRecord uniqueness: { case_sensitive: false } include Tokenable + + def solved_challenges + submissions.valid + end end diff --git a/spec/factories.rb b/spec/factories.rb index ae13e2f5..829723ee 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -27,7 +27,7 @@ end factory :category do - name { 'web' } + sequence(:name) { |n| "category-#{n}" } end factory :challenge do @@ -53,7 +53,7 @@ factory :submission do challenge - category + category { challenge.category } flag { 'flag{invalid}' } user team diff --git a/spec/models/challenge_spec.rb b/spec/models/challenge_spec.rb index d542761d..cc738761 100644 --- a/spec/models/challenge_spec.rb +++ b/spec/models/challenge_spec.rb @@ -63,4 +63,13 @@ expect(@challenge.description.to_md).to eq("

Finish the challenge to get the flag

\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 diff --git a/spec/models/submission_spec.rb b/spec/models/submission_spec.rb index ab9411f9..2dbda29c 100644 --- a/spec/models/submission_spec.rb +++ b/spec/models/submission_spec.rb @@ -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 diff --git a/spec/models/team_spec.rb b/spec/models/team_spec.rb index 0f0e83d7..93cdd442 100644 --- a/spec/models/team_spec.rb +++ b/spec/models/team_spec.rb @@ -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