diff --git a/app/api/repp/contact_v1.rb b/app/api/repp/contact_v1.rb index f613d8f22..52403fb2d 100644 --- a/app/api/repp/contact_v1.rb +++ b/app/api/repp/contact_v1.rb @@ -4,11 +4,24 @@ module Repp resource :contacts do desc 'Return list of contact' + params do + optional :limit, type: Integer, values: (1..20).to_a + optional :offset, type: Integer + end + get '/' do - contacts = current_user.registrar.contacts.page(params[:page]) + limit = params[:limit] || 20 + offset = params[:offset] || 0 + + if params[:details] == 'true' + contacts = current_user.registrar.contacts.limit(limit).offset(offset) + else + contacts = current_user.registrar.contacts.limit(limit).offset(offset).pluck(:code) + end + @response = { contacts: contacts, - total_pages: contacts.total_pages + total_number_of_records: current_user.registrar.contacts.count } end end diff --git a/spec/requests/contact_spec.rb b/spec/requests/contact_spec.rb index 7fa1b5006..e9c9c4112 100644 --- a/spec/requests/contact_spec.rb +++ b/spec/requests/contact_spec.rb @@ -4,30 +4,79 @@ describe Repp::ContactV1 do before :all do create_settings @api_user = Fabricate(:gitlab_api_user) + Fabricate.times(2, :contact, registrar: @api_user.registrar) + Fabricate.times(2, :contact) end describe 'GET /repp/v1/contacts', autodoc: true do it 'returns contacts of the current registrar' do - Fabricate.times(2, :contact, registrar: @api_user.registrar) - Fabricate.times(2, :contact) - - get_with_auth '/repp/v1/contacts', { page: 1 }, @api_user + get_with_auth '/repp/v1/contacts', { limit: 1, details: true }, @api_user expect(response.status).to eq(200) body = JSON.parse(response.body) - expect(body['total_pages']).to eq(1) + body['total_number_of_records'].should == 2 # TODO: Maybe there is a way not to convert from and to json again - expect(body['contacts'].to_json).to eq(@api_user.registrar.contacts.to_json) + expect(body['contacts'].to_json).to eq(@api_user.registrar.contacts.limit(1).to_json) - log = ApiLog::ReppLog.first + log = ApiLog::ReppLog.last expect(log[:request_path]).to eq('/repp/v1/contacts') expect(log[:request_method]).to eq('GET') - expect(log[:request_params]).to eq('{"page":"1"}') + expect(log[:request_params]).to eq('{"limit":1,"details":"true"}') expect(log[:response].length).to be > 20 expect(log[:response_code]).to eq('200') expect(log[:api_user_name]).to eq('gitlab') expect(log[:ip]).to eq('127.0.0.1') end + + it 'returns contact names with offset' do + get_with_auth '/repp/v1/contacts', { offset: 1 }, @api_user + expect(response.status).to eq(200) + + body = JSON.parse(response.body) + body['total_number_of_records'].should == 2 + + # TODO: Maybe there is a way not to convert from and to json again + expect(body['contacts'].to_json).to eq(@api_user.registrar.contacts.offset(1).pluck(:code).to_json) + + log = ApiLog::ReppLog.last + expect(log[:request_path]).to eq('/repp/v1/contacts') + expect(log[:request_method]).to eq('GET') + expect(log[:request_params]).to eq('{"offset":1}') + expect(log[:response].length).to be > 20 + expect(log[:response_code]).to eq('200') + expect(log[:api_user_name]).to eq('gitlab') + expect(log[:ip]).to eq('127.0.0.1') + end + + it 'returns contact names of the current registrar' do + get_with_auth '/repp/v1/contacts', {}, @api_user + expect(response.status).to eq(200) + + body = JSON.parse(response.body) + body['total_number_of_records'].should == 2 + + # TODO: Maybe there is a way not to convert from and to json again + expect(body['contacts'].to_json).to eq(@api_user.registrar.contacts.pluck(:code).to_json) + + log = ApiLog::ReppLog.last + expect(log[:request_path]).to eq('/repp/v1/contacts') + expect(log[:request_method]).to eq('GET') + expect(log[:request_params]).to eq('{}') + expect(log[:response].length).to be > 20 + expect(log[:response_code]).to eq('200') + expect(log[:api_user_name]).to eq('gitlab') + expect(log[:ip]).to eq('127.0.0.1') + end + + it 'returns an error with invalid parameters in contact index' do + get_with_auth '/repp/v1/contacts', { limit: 0 }, @api_user + expect(response.status).to eq(400) + + body = JSON.parse(response.body) + body['error'].should == 'limit does not have a valid value' + + # TODO: Log failed API requests too + end end end diff --git a/spec/requests/domain_spec.rb b/spec/requests/domain_spec.rb index ddcfc6ee1..d1d17b819 100644 --- a/spec/requests/domain_spec.rb +++ b/spec/requests/domain_spec.rb @@ -77,14 +77,7 @@ describe Repp::DomainV1 do body = JSON.parse(response.body) body['error'].should == 'limit does not have a valid value' - log = ApiLog::ReppLog.last - log[:request_path].should == '/repp/v1/domains' - log[:request_method].should == 'GET' - log[:request_params].should == '{}' - log[:response_code].should == '200' - log[:api_user_name].should == 'gitlab' - log[:api_user_registrar].should == 'registrar1' - log[:ip].should == '127.0.0.1' + # TODO: Log failed API requests too end end end