Skip to content

Commit

Permalink
Add more tests for all the API "index" methods. Small search change.
Browse files Browse the repository at this point in the history
This adds a basic test suite across all the API "index" methods that
are used in DataTables to ensure they behave as DataTables expects
(pagination, searching, etc).

To make testing easier, also add searching for the "_id" field to all
the searching.

Also adjust "_id" searching to only match the full value, rather than
wildcards (since I'm not sure we would ever really need wildcard
searches for IDs, and this will make searches a bit easier under the
Postgres stuff we're exploring with the explicit UUID type).

To make testing easier, also adjust how the datatables "order" param
gets parsed to allow for missing indexes (even though this shouldn't
really be happening in the real world). But this makes it easier to
test to ensure indexes are looped over in numeric order (so we can send
in "2" and "12" to test this).
  • Loading branch information
GUI committed Jul 3, 2017
1 parent 9427524 commit e936932
Show file tree
Hide file tree
Showing 21 changed files with 826 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def index
if(params[:search] && params[:search][:value].present?)
@admin_groups = @admin_groups.or([
{ :name => /#{Regexp.escape(params[:search][:value])}/i },
{ :_id => params[:search][:value].downcase },
])
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def index
{ :email => /#{Regexp.escape(params["search"]["value"])}/i },
{ :username => /#{Regexp.escape(params["search"]["value"])}/i },
{ :authentication_token => /#{Regexp.escape(params["search"]["value"])}/i },
{ :_id => /#{Regexp.escape(params["search"]["value"])}/i },
{ :_id => params[:search][:value].downcase },
])
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def index
{ :name => /#{Regexp.escape(params[:search][:value])}/i },
{ :host => /#{Regexp.escape(params[:search][:value])}/i },
{ :path_prefix => /#{Regexp.escape(params[:search][:value])}/i },
{ :_id => params[:search][:value].downcase },
])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def index
{ :api_key => /#{Regexp.escape(params["search"]["value"])}/i },
{ :registration_source => /#{Regexp.escape(params["search"]["value"])}/i },
{ :roles => /#{Regexp.escape(params["search"]["value"])}/i },
{ :_id => /#{Regexp.escape(params["search"]["value"])}/i },
{ :_id => params[:search][:value].downcase },
])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def index
@website_backends = @website_backends.or([
{ :frontend_host => /#{Regexp.escape(params[:search][:value])}/i },
{ :server_host => /#{Regexp.escape(params[:search][:value])}/i },
{ :_id => params[:search][:value].downcase },
])
end
end
Expand Down
8 changes: 3 additions & 5 deletions src/api-umbrella/web-app/app/helpers/datatables_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ def param_index_array(key)
if params[key].is_a?(Array)
as_array = params[key]
elsif params[key].is_a?(Hash)
upper_bound = params[key].length - 1
(0..upper_bound).each do |idx|
if params[key].key?(idx.to_s)
as_array << params[key][idx.to_s]
end
indexes = params[key].keys.sort_by { |k| k.to_i }
indexes.each do |index|
as_array << params[key][index]
end
elsif params.key?(key)
as_array = [params[key]]
Expand Down
2 changes: 1 addition & 1 deletion test/admin_ui/test_api_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_create
end

def test_update
api_scope = FactoryGirl.create(:api_scope)
api_scope = FactoryGirl.create(:api_scope, :name => "Example", :path_prefix => "/example")

admin_login
visit("/admin/#/api_scopes/#{api_scope.id}/edit")
Expand Down
114 changes: 114 additions & 0 deletions test/apis/v1/admin_groups/test_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def setup
AdminGroup.delete_all
end

include ApiUmbrellaSharedTests::DataTablesApi

def test_admin_usernames_in_group
group = FactoryGirl.create(:admin_group)
admin_in_group = FactoryGirl.create(:limited_admin, :groups => [
Expand Down Expand Up @@ -51,4 +53,116 @@ def test_admin_usernames_empty
assert_equal(1, data["data"].length)
assert_equal([], data["data"][0]["admin_usernames"])
end

def test_response_fields
record = FactoryGirl.create(data_tables_factory_name, {
:created_at => Time.utc(2017, 1, 1),
:created_by => SecureRandom.uuid,
:name => "Example",
:permission_ids => ["analytics", "user_view"],
:updated_at => Time.utc(2017, 1, 2),
:updated_by => SecureRandom.uuid,
})
admin = FactoryGirl.create(:admin, :groups => [record])

response = Typhoeus.get(data_tables_api_url, http_options.deep_merge(admin_token).deep_merge({
:params => {
:search => { :value => record.id },
},
}))
assert_response_code(200, response)
data = MultiJson.load(response.body)
assert_data_tables_root_fields(data)
assert_equal(1, data.fetch("data").length)

record_data = data.fetch("data").first
assert_base_record_fields(record_data)

assert_equal(1, record_data.fetch("admin_usernames").length)
assert_equal(admin.username, record_data.fetch("admin_usernames").first)
assert_equal(1, record_data.fetch("api_scope_display_names").length)
assert_match("- localhost/example", record_data.fetch("api_scope_display_names").first)
assert_equal(1, record_data.fetch("api_scope_ids").length)
assert_match_uuid(record_data.fetch("api_scope_ids").first)
assert_equal("2017-01-01T00:00:00Z", record_data.fetch("created_at"))
assert_match_uuid(record_data.fetch("created_by"))
assert_equal(record.created_by, record_data.fetch("created_by"))
assert_equal("Example", record_data.fetch("name"))
assert_equal(["Analytics", "API Users - View"], record_data.fetch("permission_display_names"))
assert_equal(["analytics", "user_view"], record_data.fetch("permission_ids"))
assert_equal("2017-01-02T00:00:00Z", record_data.fetch("updated_at"))
assert_match_uuid(record_data.fetch("updated_by"))
assert_equal(record.updated_by, record_data.fetch("updated_by"))
end

def test_empty_response_fields
record = FactoryGirl.create(data_tables_factory_name)

response = Typhoeus.get(data_tables_api_url, http_options.deep_merge(admin_token).deep_merge({
:params => {
:search => { :value => record.id },
},
}))
assert_response_code(200, response)
data = MultiJson.load(response.body)
assert_data_tables_root_fields(data)
assert_equal(1, data.fetch("data").length)

record_data = data.fetch("data").first
assert_base_record_fields(record_data)

assert_nil(record_data.fetch("created_by"))
assert_nil(record_data.fetch("updated_by"))
end

def test_search_name
assert_data_tables_search(:name, "NameSearchTest", "amesearcht")
end

def test_order_name
assert_data_tables_order(:name, ["A", "B"])
end

private

def data_tables_api_url
"https://127.0.0.1:9081/api-umbrella/v1/admin_groups.json"
end

def data_tables_factory_name
:admin_group
end

def data_tables_record_count
AdminGroup.where(:deleted_at => nil).count
end

def assert_base_record_fields(record_data)
assert_equal([
"admin_usernames",
"api_scope_display_names",
"api_scope_ids",
"created_at",
"created_by",
"deleted_at",
"id",
"name",
"permission_display_names",
"permission_ids",
"updated_at",
"updated_by",
"version",
].sort, record_data.keys.sort)
assert_kind_of(Array, record_data.fetch("admin_usernames"))
assert_kind_of(Array, record_data.fetch("api_scope_display_names"))
assert_kind_of(Array, record_data.fetch("api_scope_ids"))
assert_match_iso8601(record_data.fetch("created_at"))
assert_nil(record_data.fetch("deleted_at"))
assert_match_uuid(record_data.fetch("id"))
assert_kind_of(String, record_data.fetch("name"))
assert_kind_of(Array, record_data.fetch("permission_display_names"))
assert_kind_of(Array, record_data.fetch("permission_ids"))
assert_match_iso8601(record_data.fetch("updated_at"))
assert_kind_of(Integer, record_data.fetch("version"))
end
end
21 changes: 11 additions & 10 deletions test/apis/v1/admins/test_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ def setup
Admin.delete_all
end

def test_paginate_results
FactoryGirl.create_list(:admin, 3)
include ApiUmbrellaSharedTests::DataTablesApi

response = Typhoeus.get("https://127.0.0.1:9081/api-umbrella/v1/admins.json?length=2", http_options.deep_merge(admin_token))
assert_response_code(200, response)
private

admin_count = Admin.where(:deleted_at => nil).count
assert_operator(admin_count, :>, 2)
def data_tables_api_url
"https://127.0.0.1:9081/api-umbrella/v1/admins.json"
end

def data_tables_factory_name
:admin
end

data = MultiJson.load(response.body)
assert_equal(admin_count, data["recordsTotal"])
assert_equal(admin_count, data["recordsFiltered"])
assert_equal(2, data["data"].length)
def data_tables_record_count
Admin.where(:deleted_at => nil).count
end
end
130 changes: 130 additions & 0 deletions test/apis/v1/api_scopes/test_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
require_relative "../../../test_helper"

class Test::Apis::V1::ApiScopes::TestIndex < Minitest::Test
include ApiUmbrellaTestHelpers::AdminAuth
include ApiUmbrellaTestHelpers::Setup

def setup
super
setup_server
ApiScope.delete_all
end

include ApiUmbrellaSharedTests::DataTablesApi

def test_response_fields
record = FactoryGirl.create(data_tables_factory_name, {
:created_at => Time.utc(2017, 1, 1),
:created_by => SecureRandom.uuid,
:host => "example.com",
:name => "Example",
:path_prefix => "/#{unique_test_id}/",
:updated_at => Time.utc(2017, 1, 2),
:updated_by => SecureRandom.uuid,
})

response = Typhoeus.get(data_tables_api_url, http_options.deep_merge(admin_token).deep_merge({
:params => {
:search => { :value => record.id },
},
}))
assert_response_code(200, response)
data = MultiJson.load(response.body)
assert_data_tables_root_fields(data)
assert_equal(1, data.fetch("data").length)

record_data = data.fetch("data").first
assert_base_record_fields(record_data)

assert_equal("2017-01-01T00:00:00Z", record_data.fetch("created_at"))
assert_match_uuid(record_data.fetch("created_by"))
assert_equal(record.created_by, record_data.fetch("created_by"))
assert_equal("example.com", record_data.fetch("host"))
assert_equal("Example", record_data.fetch("name"))
assert_equal("/#{unique_test_id}/", record_data.fetch("path_prefix"))
assert_equal("2017-01-02T00:00:00Z", record_data.fetch("updated_at"))
assert_match_uuid(record_data.fetch("updated_by"))
assert_equal(record.updated_by, record_data.fetch("updated_by"))
end

def test_empty_response_fields
record = FactoryGirl.create(data_tables_factory_name)

response = Typhoeus.get(data_tables_api_url, http_options.deep_merge(admin_token).deep_merge({
:params => {
:search => { :value => record.id },
},
}))
assert_response_code(200, response)
data = MultiJson.load(response.body)
assert_data_tables_root_fields(data)
assert_equal(1, data.fetch("data").length)

record_data = data.fetch("data").first
assert_base_record_fields(record_data)

assert_nil(record_data.fetch("created_by"))
assert_nil(record_data.fetch("updated_by"))
end

def test_search_name
assert_data_tables_search(:name, "NameSearchTest", "amesearcht")
end

def test_search_host
assert_data_tables_search(:host, "hostsearchtest.com", "ostsearcht")
end

def test_search_path_prefix
assert_data_tables_search(:path_prefix, "/path-prefix/search-test/", "refix/searc")
end

def test_order_name
assert_data_tables_order(:name, ["A", "B"])
end

def test_order_host
assert_data_tables_order(:host, ["a.example.com", "b.example.com"])
end

def test_order_path_prefix
assert_data_tables_order(:path_prefix, ["/a", "/b"])
end

private

def data_tables_api_url
"https://127.0.0.1:9081/api-umbrella/v1/api_scopes.json"
end

def data_tables_factory_name
:api_scope
end

def data_tables_record_count
ApiScope.where(:deleted_at => nil).count
end

def assert_base_record_fields(record_data)
assert_equal([
"created_at",
"created_by",
"deleted_at",
"host",
"id",
"name",
"path_prefix",
"updated_at",
"updated_by",
"version",
].sort, record_data.keys.sort)
assert_match_iso8601(record_data.fetch("created_at"))
assert_nil(record_data.fetch("deleted_at"))
assert_kind_of(String, record_data.fetch("host"))
assert_match_uuid(record_data.fetch("id"))
assert_kind_of(String, record_data.fetch("name"))
assert_kind_of(String, record_data.fetch("path_prefix"))
assert_match_iso8601(record_data.fetch("updated_at"))
assert_kind_of(Integer, record_data.fetch("version"))
end
end
30 changes: 10 additions & 20 deletions test/apis/v1/apis/test_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,19 @@ def setup
Api.delete_all
end

def test_datatables_output_fields
response = Typhoeus.get("https://127.0.0.1:9081/api-umbrella/v1/apis.json", http_options.deep_merge(admin_token))
assert_response_code(200, response)
include ApiUmbrellaSharedTests::DataTablesApi

data = MultiJson.load(response.body)
assert_equal([
"data",
"draw",
"recordsFiltered",
"recordsTotal",
], data.keys.sort)
end
private

def test_paginate_results
FactoryGirl.create_list(:api, 3)
response = Typhoeus.get("https://127.0.0.1:9081/api-umbrella/v1/apis.json?length=2", http_options.deep_merge(admin_token))
assert_response_code(200, response)
def data_tables_api_url
"https://127.0.0.1:9081/api-umbrella/v1/apis.json"
end

assert_equal(3, Api.where(:deleted_at => nil).count)
def data_tables_factory_name
:api
end

data = MultiJson.load(response.body)
assert_equal(3, data["recordsTotal"])
assert_equal(3, data["recordsFiltered"])
assert_equal(2, data["data"].length)
def data_tables_record_count
Api.where(:deleted_at => nil).count
end
end
Loading

0 comments on commit e936932

Please sign in to comment.