Module: Glib::TestHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/glib/test_helpers.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

HOST =

The best practice is to avoid using lvh.me due to security and performance concerns.

'www.localhost:3000'

Instance Method Summary collapse

Instance Method Details

#crawl_json_pages(user, log_file: nil, dump_actions: false, dump_path: nil, skip_similar_page: false, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/glib/test_helpers.rb', line 39

def crawl_json_pages(user, log_file: nil, dump_actions: false, dump_path: nil, skip_similar_page: false, &block)
  __execute_crawler(user, inspect_http: true) do |router, http|
    path = user[:path] ? "#{user[:path]}?format=json" : '/users/me?format=json&redirect=default'
    router.host = HOST
    router.skip_similar_page = skip_similar_page
    router.step(
      http,
      'onClick' => {
        'action' => user[:action] || 'initiate_navigation',
        'url' => user[:url] || "http://#{HOST}#{path}"
      }
    )

    if dump_actions
      csv_string = router.http_actions.map do |row|
        action, url, params = row
        [action, url, JSON.generate(params)].to_csv
      end.join

      filepath = crawler_output_file(user, file_dir: dump_path)
      File.write(filepath, csv_string)
    end
  end
end

#crawler_output_file(user, file_dir: nil) ⇒ Object



64
65
66
67
# File 'lib/glib/test_helpers.rb', line 64

def crawler_output_file(user, file_dir: nil)
  filename = "#{user[:email]}[#{user[:device]}][#{user[:version] || 'current'}].csv"
  File.join(file_dir || __crawler_log_dir, filename)
end

#glib_travel(*args, &block) ⇒ Object



138
139
140
# File 'lib/glib/test_helpers.rb', line 138

def glib_travel(*args, &block)
  Timecop.travel(*args, &block)
end

#glib_travel_backObject



146
147
148
# File 'lib/glib/test_helpers.rb', line 146

def glib_travel_back
  Timecop.return
end

#glib_travel_freeze(*args, &block) ⇒ Object



142
143
144
# File 'lib/glib/test_helpers.rb', line 142

def glib_travel_freeze(*args, &block)
  Timecop.freeze(*args, &block)
end

#logoutObject



77
78
79
80
# File 'lib/glib/test_helpers.rb', line 77

def logout
  delete logout_url
  assert_response :success
end

#logout_urlObject



82
83
84
# File 'lib/glib/test_helpers.rb', line 82

def logout_url
  "http://#{HOST}/users/sign_out.json"
end

#response_assert_equalObject

LOG_DIR = File.expand_path(

File.join(File.dirname(__FILE__), 'integration/json_ui_crawler_test_results')

)



33
34
35
36
37
# File 'lib/glib/test_helpers.rb', line 33

def response_assert_equal
  expected = __get_previous_result_from_git
  result = __log_controller_data(response.body)
  assert_equal JSON.parse(expected), JSON.parse(result), "Result mismatch! #{__git_is_available? ? `git diff #{__controller_log_dir}/#{__controller_log_file}` : ''}"
end

#retrace_json_pages(user, past_actions:) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/glib/test_helpers.rb', line 69

def retrace_json_pages(user, past_actions:)
  __execute_crawler(user, inspect_http: false) do |router, http|
    # router.follow(http, guiding_routers)
    # router.follow_v2(Glib::JsonCrawler::Http.new(self, user, router), actions)
    router.follow_v2(http, past_actions)
  end
end

#submit_form(page_payload, data, url: nil, method: nil) ⇒ Object

Submits a form by parsing the page payload and extracting form data. This ensures the view renders successfully before submitting.

Examples:

get new_chat_url(checklist_id: @checklist.id), params: json_params
submit_form(response.body, { chat: { message_body: 'Hello' } })

Parameters:

  • page_payload (String)

    The JSON response body containing the form

  • data (Hash)

    The form data to submit (e.g., { chat: { message_body: ‘hello’ } })

  • url (String, nil) (defaults to: nil)

    Optional URL override (if form URL is nil)

  • method (Symbol, nil) (defaults to: nil)

    Optional HTTP method override



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/glib/test_helpers.rb', line 98

def submit_form(page_payload, data, url: nil, method: nil)
  json = JSON.parse(page_payload)
  form = __find_form(json)

  raise 'No form found in page. Ensure the form is rendered properly.' unless form

  # Get URL from form, or use provided URL
  form_url = form['url']
  if form_url.blank? && form['onSubmit']
    on_submit = form['onSubmit']
    form_url = on_submit['httpPost']&.[]('url') || on_submit['http']&.[]('url')
  end
  url ||= form_url

  raise "Form missing 'url'. Please provide url: parameter. Form keys: #{form.keys.join(', ')}" if url.blank?

  # Get method from parameter, form, or default to post
  method ||= form['method']
  method = 'post' if method.blank?

  # Build params: hidden fields from form + provided data
  params = __build_form_params(form, data)

  # Submit using the appropriate HTTP method
  case method.to_sym
  when :get
    get url, params: params
  when :post
    post url, params: params
  when :patch
    patch url, params: params
  when :put
    put url, params: params
  else
    send(method, url, params: params)
  end

  assert_response :success
end