From dd17f1d5ad61c5bed8eb503f5809600f18abe169 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 12 Oct 2018 17:43:36 +0200 Subject: [PATCH] Add project READ endpoints [SCI-2742] --- .../api/v1/projects_controller_spec.rb | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 spec/requests/api/v1/projects_controller_spec.rb diff --git a/spec/requests/api/v1/projects_controller_spec.rb b/spec/requests/api/v1/projects_controller_spec.rb new file mode 100644 index 000000000..34c3c556a --- /dev/null +++ b/spec/requests/api/v1/projects_controller_spec.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe "Api::V1::ProjectsController", type: :request do + before :all do + @user = create(:user) + @teams = create_list(:team, 2, created_by: @user) + create(:user_team, user: @user, team: @teams.first, role: 2) + + # valid_projects + create(:project, name: Faker::Name.unique.name, + created_by: @user, team: @teams.first) + create(:project, name: Faker::Name.unique.name, + created_by: @user, team: @teams.first) + + # unaccessable_projects + create(:project, name: Faker::Name.unique.name, + created_by: @user, team: @teams.second) + create(:project, name: Faker::Name.unique.name, + created_by: @user, team: @teams.second) + + @valid_headers = + { 'Authorization': 'Bearer ' + generate_token(@user.id) } + end + + describe 'GET projects, #index' do + it 'Response with correct projects' do + hash_body = nil + get api_v1_team_projects_path(team_id: @teams.first.id), + headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(@teams.first.projects, + each_serializer: Api::V1::ProjectSerializer) + .as_json[:data] + ) + end + + it 'When invalid request, user in not member of the team' do + hash_body = nil + get api_v1_team_projects_path(team_id: @teams.second.id), + headers: @valid_headers + expect(response).to have_http_status(403) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + + it 'When invalid request, non existing team' do + hash_body = nil + get api_v1_team_projects_path(team_id: -1), headers: @valid_headers + expect(response).to have_http_status(404) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + end + + describe 'GET project, #show' do + it 'When valid request, user can read project' do + hash_body = nil + get api_v1_team_project_path(team_id: @teams.first.id, + id: @teams.first.projects.first.id), + headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(@teams.first.projects.first, + serializer: Api::V1::ProjectSerializer) + .as_json[:data] + ) + end + + it 'When invalid request, user in not member of the team' do + hash_body = nil + get api_v1_team_project_path(team_id: @teams.second.id, + id: @teams.second.projects.first.id), + headers: @valid_headers + expect(response).to have_http_status(403) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + + it 'When invalid request, non existing project' do + hash_body = nil + get api_v1_team_project_path(team_id: @teams.first.id, id: -1), + headers: @valid_headers + expect(response).to have_http_status(404) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + + it 'When invalid request, project from another team' do + hash_body = nil + get api_v1_team_project_path(team_id: @teams.first.id, + id: @teams.second.projects.first.id), + headers: @valid_headers + expect(response).to have_http_status(404) + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + end +end