Skip to content
Snippets Groups Projects
Select Git revision
  • 86027ea33d1be6d7e5cb926087e1f873460bcd3b
  • eip-develop default
  • ovalt/user-restore
  • develop
  • 0valt/1865/filters
  • 0valt/1885/flag-history
  • 0valt/keyboard
  • art/complaints
  • 0valt/1875/audit-logs
  • 0valt/1876/thread-rename
  • dependabot/npm_and_yarn/npm_and_yarn-a48ea45f8c
  • cellio/714-rename-thread
  • 0valt/sidebar
  • dependabot/bundler/bundler-5a9ea26a4b
  • 0valt/audits
  • 0valt/1859/tag-drafts
  • 0valt/1253/imports-filters
  • 0valt/1857/markdown-in-titles
  • 0valt/seed
  • 0valt/tags
  • 0valt/z-index
  • 0valt/docs
  • v0.12.3
  • v0.12.2
  • v0.12.1
  • v0.12.0
  • v0.11.0
  • v0.10.0
  • v0.9.0
  • v0.8.0
  • v0.7.0
  • v0.6.1
  • v0.6.0
  • v0.5.0
  • v0.4.0
  • v0.3.0
  • v1.0
37 results

badge_great_question_test.rb

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    badge_great_question_test.rb 1.70 KiB
    require 'test_helper'
    
    class BadgeGreatQuestionTest < ActionController::TestCase
      BRONZE_VOTES = 3
      SILVER_VOTES = 5
      GOLD_VOTES = 7
    
      def setup
        @post = posts(:question_no_up_votes)
        @user = @post.user
        @badge = Badge.find_by!(id: BadgeGreatQuestion.badge_id)
      end
    
      test 'user receives correct badge type when post reaches score thresholds' do
        create_votes(1, BRONZE_VOTES, @post)
        check_score_and_badge_type(BadgeGreatQuestion.badge_award_thresholds[0], 'bronze')
    
        create_votes(BRONZE_VOTES + 1, SILVER_VOTES, @post)
        check_score_and_badge_type(BadgeGreatQuestion.badge_award_thresholds[1], 'silver')
    
        create_votes(SILVER_VOTES + 1, GOLD_VOTES, @post)
        check_score_and_badge_type(BadgeGreatQuestion.badge_award_thresholds[2], 'gold')
      end
    
      test 'user does not receive badge when post does not reach votes threshold' do
        create_votes(1, BRONZE_VOTES - 1, @post)
    
        assert_equal @post.score < BadgeGreatQuestion.badge_award_thresholds[0], true
        assert_equal @user.user_badges.where(badge_id: @badge.id, badge_source: @post).count, 0
      end
    
      def create_votes(start, stop, post)
        (start..stop).each do |i|
          unique_user = users("user_test_#{i}")
          vote_attributes = votes(:one).attributes
          vote_attributes.delete('id')
          vote_attributes['user_id'] = unique_user.id
          vote_attributes['post_id'] = post.id
          post.votes.create!(vote_attributes)
        end
      end
    
      def check_score_and_badge_type(score, badge_type)
        assert_equal @post.score >= score, true
        assert_equal @user.user_badges.where(badge_id: @badge.id, badge_source: @post).count, 1
        assert_equal @user.user_badges.where(badge_id: @badge.id, badge_source: @post).first.badge_type, badge_type
      end
    end