<commit_before>require 'rails_helper'

RSpec.describe "Shifts", :type => :request do
  describe "POST /opt_in" do
    it 'creates a participation' do
      shift = create(:shift, :skip_validate)
      post "/api/v1/shifts/opt_in", params: { shift_id: shift.id }, 
                                    headers: token_header,
                                    as: :json

      expect(response).to be_success
      expect(json).to include_json({opted_id: true})
      expect(Participation.find_by(shift_id: shift.id, user_id: User.first.id)).to be_a Participation
    end
  end

  describe "POST /opt_out" do
    it 'deletes a participation' do
      shift = create(:shift, :skip_validate)
      post "/api/v1/shifts/opt_in", params: { shift_id: shift.id }, 
                                    headers: token_header,
                                    as: :json

      post "/api/v1/shifts/opt_out", params: { shift_id: shift.id }, 
                                     headers: {"Authorization": "Token token=" + response.headers["TOKEN"]},
                                     as: :json

      expect(response).to be_success
      expect(json).to include_json({opted_out: true})
      expect(Participation.find_by(shift_id: shift.id, user_id: User.first.id)).to be nil
    end
  end
end
<commit_msg>handles non existing shift properly, when trying to opt out<commit_after>require 'rails_helper'

RSpec.describe "Shifts", :type => :request do
  describe "POST /opt_in" do
    it 'creates a participation' do
      shift = create(:shift, :skip_validate)
      post "/api/v1/shifts/opt_in", params: { shift_id: shift.id }, 
                                    headers: token_header,
                                    as: :json

      expect(response).to be_success
      expect(json).to include_json({opted_id: true})
      expect(Participation.find_by(shift_id: shift.id, user_id: User.first.id)).to be_a Participation
    end
  end

  describe "POST /opt_out" do
    it 'deletes a participation' do
      shift = create(:shift, :skip_validate)
      post "/api/v1/shifts/opt_in", params: { shift_id: shift.id }, 
                                    headers: token_header,
                                    as: :json

      post "/api/v1/shifts/opt_out", params: { shift_id: shift.id }, 
                                     headers: {"Authorization": "Token token=" + response.headers["TOKEN"]},
                                     as: :json

      expect(response).to be_success
      expect(json).to include_json({opted_out: true})
      expect(Participation.find_by(shift_id: shift.id, user_id: User.first.id)).to be nil
    end

    it 'captures the situation of a non exsting shift' do
      post "/api/v1/shifts/opt_out", params: { shift_id: 42 }, 
                                     headers: token_header,
                                     as: :json
      expect(response).to be_not_found
      expect(json).to include_json({opted_out: false})
    end
  end
end

{'fraction_non_alphanumeric': 0.10805369127516778, 'fraction_numerical': 0.0030201342281879194, 'mean_word_length': 1.8691049085659288, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5545980', 'n_tokens_mistral': 898, 'n_tokens_neox': 851, 'n_words': 210}