From 297016e799ce5c7882eca6e432ebd62d68fad606 Mon Sep 17 00:00:00 2001 From: Pascal Date: Sun, 7 Mar 2021 19:11:49 -0500 Subject: [PATCH] [add] added query string support --- app/controllers/clients_controller.rb | 4 ++- spec/requests/clients_request_spec.rb | 37 +++++++++------------------ spec/support/request_spec_helper.rb | 9 +++++++ spec/support/request_spec_shared.rb | 29 +++++++++++++++++++++ 4 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 spec/support/request_spec_shared.rb diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index bb1537e..d581b59 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -178,10 +178,12 @@ class ClientsController < ApplicationController # check if the api is valid and has the appropriate prefix def parse_api_key_header # TODO: move api_key checks and formatting to helper class - # check if api key is present and can be split into 3 parts: .. + # check if api key is present in header or param and can be split into 3 parts: .. # NOTE: .compact is more for got measure than really necessary, but this ensures no nil values in the array api_key_header = request.headers[RynDNS::Application::API_KEY_HEADER].split('.').compact unless request.headers[RynDNS::Application::API_KEY_HEADER].nil? + api_key_header ||= params[RynDNS::Application::API_KEY_HEADER].split('.').compact if + params.has_key?(RynDNS::Application::API_KEY_HEADER) return head(401) if api_key_header.nil? || api_key_header.length < 3 prefix, @name_id, @api_key = api_key_header diff --git a/spec/requests/clients_request_spec.rb b/spec/requests/clients_request_spec.rb index 87e041a..41a7eac 100644 --- a/spec/requests/clients_request_spec.rb +++ b/spec/requests/clients_request_spec.rb @@ -12,7 +12,7 @@ RSpec.describe 'Clients', type: :request do let(:valid_client) do { name: 'clientA', - description: 'Client across the river', + description: 'Client&across the=river', # added &= for query string test permission: 'rw', public_ip: false } @@ -78,31 +78,18 @@ RSpec.describe 'Clients', type: :request do context 'when sending a valid request' do before { post '/admin/client', params: valid_client, headers: api_key_header(admin_client) } - it 'creates client that can read/write and returns 201' do - expect(json_response).not_to be_empty - expect(json_response['name']).to eq valid_client[:name] - expect(json_response['api_key'].length).to eq 82 - expect(response).to have_http_status(201) - api_key = json_response['api_key'] - # check if request was parsed properly - get "/admin/clients/#{valid_client[:name]}", headers: api_key_header(admin_client) - expect(json_response).not_to be_empty - expect(json_response['name']).to eq valid_client[:name] - expect(json_response['description']).to eq valid_client[:description] - expect(json_response['permission']).to eq valid_client[:permission] - expect(json_response['public_ip']).to eq valid_client[:public_ip] - expect(response).to have_http_status(200) - # test the new client write - put '/client', headers: api_key_header(nil, api_key) - expect(json_response).not_to be_empty - expect(response).to have_http_status(200) - ipv4 = json_response['ipv4'] - # test the new client read - get "/clients/#{valid_client[:name]}", headers: api_key_header(nil, api_key) - expect(json_response).not_to be_empty - expect(response).to have_http_status(200) - expect(json_response['ipv4']).to eq ipv4 + it_behaves_like 'POST /admin/client 201' + end + + # TODO: add query string tests for all requests and use shared examples + context 'when sending a valid request as query string' do + before do + api_key = "#{RynDNS::Application::API_KEY_HEADER}=#{api_key_header(admin_client)[RynDNS::Application::API_KEY_HEADER]}" + body = hash_to_query_string(valid_client) + post "/admin/client?#{api_key}&#{body}" end + + it_behaves_like 'POST /admin/client 201' end context 'when sending a valid request without api_key' do diff --git a/spec/support/request_spec_helper.rb b/spec/support/request_spec_helper.rb index 019f4a7..7ce2daa 100644 --- a/spec/support/request_spec_helper.rb +++ b/spec/support/request_spec_helper.rb @@ -15,6 +15,15 @@ module RequestSpecHelper "#{'r' if perm_r}#{'w' if perm_w}#{'x' if perm_x}" end + def hash_to_query_string(hash) + result = '' + hash.each do |k, v| + # escape value to be uri save + result += "#{k}=#{CGI.escape(v.to_s)}&" + end + result.delete_suffix('&') + end + def list_equals_db?(client_list) return false unless Client.count == client_list.count diff --git a/spec/support/request_spec_shared.rb b/spec/support/request_spec_shared.rb new file mode 100644 index 0000000..5977349 --- /dev/null +++ b/spec/support/request_spec_shared.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'POST /admin/client 201' do + it 'creates client that can read/write and returns 201' do + expect(json_response).not_to be_empty + expect(json_response['name']).to eq valid_client[:name] + expect(json_response['api_key'].length).to eq 82 + expect(response).to have_http_status(201) + api_key = json_response['api_key'] + # check if request was parsed properly + get "/admin/clients/#{valid_client[:name]}", headers: api_key_header(admin_client) + expect(json_response).not_to be_empty + expect(json_response['name']).to eq valid_client[:name] + expect(json_response['description']).to eq valid_client[:description] + expect(json_response['permission']).to eq valid_client[:permission] + expect(json_response['public_ip']).to eq valid_client[:public_ip] + expect(response).to have_http_status(200) + # test the new client write + put '/client', headers: api_key_header(nil, api_key) + expect(json_response).not_to be_empty + expect(response).to have_http_status(200) + ipv4 = json_response['ipv4'] + # test the new client read + get "/clients/#{valid_client[:name]}", headers: api_key_header(nil, api_key) + expect(json_response).not_to be_empty + expect(response).to have_http_status(200) + expect(json_response['ipv4']).to eq ipv4 + end +end