|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class AdminAreaContactVersionsControllerIntegrationTest < ApplicationIntegrationTest |
| 4 | + setup do |
| 5 | + WebMock.allow_net_connect! |
| 6 | + sign_in users(:admin) |
| 7 | + |
| 8 | + @registrar = registrars(:bestnames) |
| 9 | + |
| 10 | + create_contact_with_history |
| 11 | + end |
| 12 | + |
| 13 | + def teardown |
| 14 | + delete_objects_once_done |
| 15 | + super |
| 16 | + end |
| 17 | + |
| 18 | + def test_search_endpoint_returns_expected_json |
| 19 | + expected_result = [{ 'id' => 1, 'display_key' => 'Test Name (test_code_775)' }] |
| 20 | + |
| 21 | + Version::ContactVersion.singleton_class.define_method(:search_by_query) { |*_args| [] } unless Version::ContactVersion.respond_to?(:search_by_query) |
| 22 | + |
| 23 | + Version::ContactVersion.stub :search_by_query, expected_result do |
| 24 | + get search_admin_contact_versions_path, params: { q: 'test_code_775' } |
| 25 | + end |
| 26 | + |
| 27 | + assert_response :ok |
| 28 | + json = JSON.parse(response.body) |
| 29 | + |
| 30 | + assert_equal expected_result, json |
| 31 | + end |
| 32 | + |
| 33 | + def test_created_at_lteq_filter_is_inclusive |
| 34 | + get admin_contact_versions_path(format: :csv), |
| 35 | + params: { q: { created_at_lteq: '2018-04-23' } } |
| 36 | + |
| 37 | + assert_response :ok |
| 38 | + assert_equal 'text/csv; charset=utf-8', response.headers['Content-Type'] |
| 39 | + assert_includes response.body, 'test_code_775', |
| 40 | + 'Expected CSV export to contain the contact version created on the same day' |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + |
| 45 | + def create_contact_with_history |
| 46 | + sql = <<-SQL.squish |
| 47 | + INSERT INTO contacts (id, name, code, email, auth_info, registrar_id) |
| 48 | + VALUES (775, 'Test Name', 'test_code_775', '[email protected]', '8b4d462aa04194ca78840a', #{@registrar.id}); |
| 49 | +
|
| 50 | + INSERT INTO log_contacts ( |
| 51 | + item_type, item_id, event, whodunnit, object, object_changes, |
| 52 | + created_at, session, children, ident_updated_at, uuid |
| 53 | + ) |
| 54 | + VALUES ( |
| 55 | + 'Contact', 775, 'update', '1-AdminUser', |
| 56 | + '{"id": 775, "code": "test_code_775", "auth_info": "8b4d462aa04194ca78840a", "registrar_id": #{@registrar.id}}', |
| 57 | + '{"some_field": ["old", "new"]}', |
| 58 | + '2018-04-23 15:50:48.113491', '2018-04-23 12:44:56', '{"legal_documents":[null]}', NULL, NULL |
| 59 | + ); |
| 60 | + SQL |
| 61 | + |
| 62 | + ActiveRecord::Base.connection.execute(sql) |
| 63 | + end |
| 64 | + |
| 65 | + def delete_objects_once_done |
| 66 | + ActiveRecord::Base.connection.execute('DELETE FROM log_contacts WHERE item_id = 775') |
| 67 | + ActiveRecord::Base.connection.execute('DELETE FROM contacts WHERE id = 775') |
| 68 | + end |
| 69 | +end |
0 commit comments