<commit_before>require 'rails_helper'

RSpec.describe Api::ContestsController, type: :controller do
  include Devise::Test::ControllerHelpers

  let(:params) do
    {
      id: contest.present? ? contest.id : Contest.pluck(:id).push(0).max.next,
      lang: lang
    }
  end

  shared_examples 'return HTTP 201 Created' do
    it 'return HTTP 201 Created' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 201
    end
  end

  shared_examples 'return HTTP 404 Not Found' do
    it 'return HTTP 404 Not Found' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 404
    end
  end

  shared_examples 'return HTTP 403 Forbidden' do
    it 'return HTTP 403 Forbidden' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 403
    end
  end

  shared_examples 'return HTTP 409 Conflict' do
    it 'return HTTP 409 Conflict' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 409
    end
  end

  describe 'GET /api/contents/:id' do
    let(:json_without_problems) do
      {
        id: contest.id,
        name: params[:lang] == 'ja' ? contest.name_ja : contest.name_en,
        description: params[:lang] == 'ja' ? contest.description_ja : contest.description_en,
        joined: user.present? && ContestRegistration.find_by(user_id: user.id).present?
      }
    end

    let(:json_with_problems) do
      json_without_problems.merge(
        problems: contest.problems.map do |problem|
          {
            id: problem.id,
            name: params[:lang] == 'ja' ? problem.name_ja : problem.name_en,
            description: params[:lang] == 'ja' ? problem.description_ja : problem.description_en,
            data_sets: problem.data_sets.map do |data_set|
              {
                id: data_set.id,
                label: data_set.label,
                score: data_set.score
              }
            end
          }
        end
      )
    end

    before do
      sign_in(user) if user
    end

    shared_examples 'return http success and json without problems' do
      it 'return http success' do
        get :show, params: params
        expect(response).to have_http_status(:success)
      end
      it 'return json without problems' do
        pending 'now implementing'
        get :show, params: params
        expect(JSON.parse(response.body)).to eq json_without_problems
      end
    end

    shared_examples 'return http success and json with problems' do
      it 'return http success' do
        pending 'now mplementing'
        get :show, params: params
        expect(response).to have_http_success(:success)
      end
      it 'return json with problems' do
        pending 'now implementing'
        get :show, params: params
        expect(JSON.parse(response.body)).to eq json_with_problems
      end
    end

    %w(ja en).each do |language|
      let(:lang) { language }

      describe 'Case 1' do
        context 'when the contest does not exist and the user does not login,' do
          let(:contest) { nil }
          let(:user)    { nil }
          it_behaves_like 'return HTTP 404 Not Found'
        end

        context 'when the contest does not exist and the user logins,' do
          let(:contest) { nil }
          let(:user)    { create(:user) }
          it_behaves_like 'return HTTP 404 Not Found'
        end
      end

      describe 'Case 2' do
        context 'when the user does not login before the contest starts,' do
          let(:contest) { create(:contest_preparing) }<commit_msg>fix typo #61<commit_after>require 'rails_helper'

RSpec.describe Api::ContestsController, type: :controller do
  include Devise::Test::ControllerHelpers

  let(:params) do
    {
      id: contest.present? ? contest.id : Contest.pluck(:id).push(0).max.next,
      lang: lang
    }
  end

  shared_examples 'return HTTP 201 Created' do
    it 'return HTTP 201 Created' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 201
    end
  end

  shared_examples 'return HTTP 404 Not Found' do
    it 'return HTTP 404 Not Found' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 404
    end
  end

  shared_examples 'return HTTP 403 Forbidden' do
    it 'return HTTP 403 Forbidden' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 403
    end
  end

  shared_examples 'return HTTP 409 Conflict' do
    it 'return HTTP 409 Conflict' do
      pending 'now implementing'
      get :show, params: params
      expect(response).to have_http_status 409
    end
  end

  describe 'GET /api/contents/:id' do
    let(:json_without_problems) do
      {
        id: contest.id,
        name: params[:lang] == 'ja' ? contest.name_ja : contest.name_en,
        description: params[:lang] == 'ja' ? contest.description_ja : contest.description_en,
        joined: user.present? && ContestRegistration.find_by(user_id: user.id).present?
      }
    end

    let(:json_with_problems) do
      json_without_problems.merge(
        problems: contest.problems.map do |problem|
          {
            id: problem.id,
            name: params[:lang] == 'ja' ? problem.name_ja : problem.name_en,
            description: params[:lang] == 'ja' ? problem.description_ja : problem.description_en,
            data_sets: problem.data_sets.map do |data_set|
              {
                id: data_set.id,
                label: data_set.label,
                score: data_set.score
              }
            end
          }
        end
      )
    end

    before do
      sign_in(user) if user
    end

    shared_examples 'return http success and json without problems' do
      it 'return http success' do
        get :show, params: params
        expect(response).to have_http_status(:success)
      end
      it 'return json without problems' do
        pending 'now implementing'
        get :show, params: params
        expect(JSON.parse(response.body)).to eq json_without_problems
      end
    end

    shared_examples 'return http success and json with problems' do
      it 'return http success' do
        pending 'now implementing'
        get :show, params: params
        expect(response).to have_http_success(:success)
      end
      it 'return json with problems' do
        pending 'now implementing'
        get :show, params: params
        expect(JSON.parse(response.body)).to eq json_with_problems
      end
    end

    %w(ja en).each do |language|
      let(:lang) { language }

      describe 'Case 1' do
        context 'when the contest does not exist and the user does not login,' do
          let(:contest) { nil }
          let(:user)    { nil }
          it_behaves_like 'return HTTP 404 Not Found'
        end

        context 'when the contest does not exist and the user logins,' do
          let(:contest) { nil }
          let(:user)    { create(:user) }
          it_behaves_like 'return HTTP 404 Not Found'
        end
      end

      describe 'Case 2' do
        context 'when the user does not login before the contest starts,' do
          let(:contest) { create(:contest_preparing) }
{'fraction_non_alphanumeric': 0.08462374354794892, 'fraction_numerical': 0.012496604183645748, 'mean_word_length': 2.2479047198941333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6338447', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2070, 'n_words': 672}