scinote-web/spec/requests/api/v1/inventories_controller_spec.rb
2018-09-26 10:47:13 -07:00

188 lines
6.7 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "Api::V1::InventoriesController", 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_inventories
create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first)
create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first)
# unaccessable_inventories
create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second)
create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second)
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
end
describe 'GET inventories, #index' do
it 'Response with correct inventories' do
hash_body = nil
get api_v1_team_inventories_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.repositories,
each_serializer: Api::V1::InventorySerializer)
.as_json[:data]
)
end
it 'When invalid request, user in not member of the team' do
hash_body = nil
get api_v1_team_inventories_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
end
describe 'GET inventory, #show' do
it 'When valid request, user is member of the team' do
hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.first.id,
id: @teams.first.repositories.first.id),
headers: @valid_headers
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(@teams.first.repositories.first,
serializer: Api::V1::InventorySerializer)
.as_json[:data]
)
end
it 'When invalid request, user in not member of the team' do
hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.second.id,
id: @teams.second.repositories.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 inventory' do
hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.first.id, id: 123),
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, repository from another team' do
hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.first.id,
id: @teams.second.repositories.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
describe 'POST inventory, #create' do
before :all do
@valid_headers['Content-Type'] = 'application/json'
@request_body = { data:
{ type: 'inventories',
attributes: {
name: Faker::Name.unique.name
} } }
end
it 'Response with correct inventory' do
hash_body = nil
post api_v1_team_inventories_path(
team_id: @teams.first.id
), params: @request_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(Repository.last,
serializer: Api::V1::InventorySerializer)
.as_json[:data]
)
end
it 'When invalid request, user in not member of the team' do
hash_body = nil
post api_v1_team_inventories_path(
team_id: @teams.second.id
), params: @request_body.to_json, 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-existent team' do
hash_body = nil
post api_v1_team_inventories_path(
team_id: 123
), params: @request_body.to_json, 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, incorrect type' do
invalid_request_body = @request_body.deep_dup
invalid_request_body[:data][:type] = 'repository_rows'
hash_body = nil
post api_v1_team_inventories_path(
team_id: @teams.first.id
), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception
expect(hash_body).to match({})
end
it 'When invalid request, missing data param' do
hash_body = nil
post api_v1_team_inventories_path(
team_id: @teams.first.id
), params: {}, headers: @valid_headers
expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception
expect(hash_body).to match({})
end
it 'When invalid request, missing attributes param' do
invalid_request_body = @request_body.deep_dup
invalid_request_body[:data].delete(:attributes)
hash_body = nil
post api_v1_team_inventories_path(
team_id: @teams.first.id
), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception
expect(hash_body).to match({})
end
it 'When invalid request, missing type param' do
invalid_request_body = @request_body.deep_dup
invalid_request_body[:data].delete(:type)
hash_body = nil
post api_v1_team_inventories_path(
team_id: @teams.first.id
), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception
expect(hash_body).to match({})
end
end
end