<commit_before>require 'spec_helper'

RSpec.describe Api::V1::ProjectRolesController, type: :controller do
  let(:user) { create(:user) }
  let(:authorized_user) { user }
  let(:project) { create(:project, owner: user) }

  let!(:acls) do
    create_list :access_control_list, 2, resource: project,
                roles: ["tester"]
  end

  let(:api_resource_name) { 'project_roles' }
  let(:api_resource_attributes) { %w(id roles) }
  let(:api_resource_links) { %w(project_roles.project) }

  let(:scopes) { %w(public project) }
  let(:resource) { acls.first }
  let(:resource_class) { AccessControlList }

  describe "#index" do
    let!(:private_resource) { create(:access_control_list, resource: create(:project, private: true)) }
    let(:n_visible) { 3 }

    context "when not logged in" do
      let(:authorized_user) { nil }

      it_behaves_like "is indexable"
    end

    describe "a logged in user" do

      it_behaves_like "is indexable"
    end
  end

  describe "#show" do
    it_behaves_like "is showable"
  end

  describe "#update" do
    let(:unauthorized_user) { create(:user) }
    let(:test_attr) { :roles }
    let(:test_attr_value) { %w(collaborator) }
    let(:update_params) do
      { project_roles: { roles: ["collaborator"] } }
    end

    it_behaves_like "is updatable"
  end

  describe "#create" do
    let(:test_attr) { :roles }
    let(:test_attr_value) { ["collaborator"] }

    let(:create_params) do
      {
        project_roles: {
          roles: ["collaborator"],
          links: {<commit_msg>spec filter by resource_id<commit_after>require 'spec_helper'

RSpec.describe Api::V1::ProjectRolesController, type: :controller do
  let(:user) { create(:user) }
  let(:authorized_user) { user }
  let(:project) { create(:project, owner: user) }

  let!(:acls) do
    create_list :access_control_list, 2, resource: project,
                roles: ["tester"]
  end

  let(:api_resource_name) { 'project_roles' }
  let(:api_resource_attributes) { %w(id roles) }
  let(:api_resource_links) { %w(project_roles.project) }

  let(:scopes) { %w(public project) }
  let(:resource) { acls.first }
  let(:resource_class) { AccessControlList }

  describe "#index" do
    let!(:private_resource) { create(:access_control_list, resource: create(:project, private: true)) }
    let(:n_visible) { 3 }

    context "when not logged in" do
      let(:authorized_user) { nil }

      it_behaves_like "is indexable"
    end

    describe "a logged in user" do

      it_behaves_like "is indexable"

      describe "filter params" do
        let!(:new_project) { create(:project) }

        before(:each) do
          get :index, index_options
        end

        describe "filter by project_id" do
          let(:index_options) { { resource_id: new_project.id } }

          it "should respond with 1 item" do
            expect(json_response[api_resource_name].length).to eq(1)
          end

          it "should respond with the correct item" do
            project_id = json_response[api_resource_name][0]['links']['project']
            expect(project_id).to eq(new_project.id.to_s)
          end
        end
      end
    end
  end

  describe "#show" do
    it_behaves_like "is showable"
  end

  describe "#update" do
    let(:unauthorized_user) { create(:user) }
    let(:test_attr) { :roles }
    let(:test_attr_value) { %w(collaborator) }
    let(:update_params) do
      { project_roles: { roles: ["collaborator"] } }
    end

    it_behaves_like "is updatable"
  end

  describe "#create" do
    let(:test_attr) { :roles }
    let(:test_attr_value) { ["collaborator"] }

    let(:create_params) do
      {
        project_roles: {
          roles: ["collaborator"],
          links: {
{'fraction_non_alphanumeric': 0.1271505376344086, 'fraction_numerical': 0.0024193548387096775, 'mean_word_length': 3.0935093509350935, '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': '3518822', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1218, 'n_words': 272}