-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tests for all the API "index" methods. Small search change.
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
Showing
21 changed files
with
826 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.