Skip to content
Snippets Groups Projects
Forked from PortalMEC / portalmec
807 commits behind the upstream repository.
institutions_spec.rb 2.97 KiB
require 'acceptance_helpers'

resource 'Institutions' do
  header 'Accept', 'application/json'
  header 'Content-Type', 'application/json'

  explanation "Institutions can be schools, universities, or any other institution."

  before { 12.times { create(:institution) } }

  let(:institutions) { Institution.all }

  get '/v1/institutions' do
    parameter :limit, 'Limit of institutions'
    parameter :offset, 'Offset of institutions'

    let(:limit) { 12 }
    let(:offset) { 0 }

    example_request 'Get a list of institutions' do
      # active model serializer may render model associations in different order for collections (array of items), so we're verifing only returned ids
      expect(JSON.parse(response_body).map { |o| o['id'] }.sort).to eq(Institution.limit(limit).offset(offset).pluck(:id).sort)
      expect(status).to eq(200)
    end
  end

  get '/v1/institutions/:id' do
    let(:id) { institutions.first.id }

    example 'Get an institution' do
      do_request
      expect(path).to eq("/v1/institutions/#{id}") # `:id` is replaced with the value of `id`
      expect(response_body).to eq(Helper.serialize(Institution.find(id)))
      expect(status).to eq(200)
    end
  end

  post '/v1/institutions' do
    include_context "authenticate_user"

    parameter :name, 'The name of the institution'
    parameter :description, 'The description of the institution'
    parameter :address, 'The address of the institution'
    parameter :city, 'The city of the institution'
    parameter :country, 'The country of the institution'

    let(:name) { Faker::Name.name }
    let(:description) { Faker::Lorem.paragraph }
    let(:address) { Faker::Address.street_address }
    let(:city) { Faker::Address.city }
    let(:country) { Faker::Address.country }
    let(:raw_post) { params.to_json }

    example 'Creating an institution' do
      do_request
      expect(status).to eq(201)
    end
  end

  put '/v1/institutions/:id' do
    include_context "authenticate_user"

    parameter :id, 'The id of the institution'
    parameter :name, 'The name of the institution'
    parameter :description, 'The description of the institution'
    parameter :address, 'The address of the institution'
    parameter :city, 'The city of the institution'
    parameter :country, 'The country of the institution'

    let(:name) { Faker::Name.name }
    let(:description) { Faker::Lorem.paragraph }
    let(:address) { Faker::Address.street_address }
    let(:city) { Faker::Address.city }
    let(:country) { Faker::Address.country }
    let(:id) { institutions.first.id }
    let(:raw_post) { params.to_json }

    example 'Updating an institution' do
      do_request
      expect(status).to eq(200)
    end
  end

  delete '/v1/institutions/:id' do
    include_context "authenticate_user"

    parameter :id, 'The id of the institution'

    let(:id) { institutions.first.id }
    let(:raw_post) { params.to_json }

    example 'Destroying an institution' do
      do_request
      expect(status).to eq(200)
    end
  end

end