<<

NAME

ProductOpener::APITest - utility functions to interact with API

DESCRIPTION

wait_dynamic_front()

Wait for dynamic_front to be ready. It's important because the application might fail because of that

wait_server()

Wait for server to be ready. It's important because the application might fail because of that

wait_application_ready()

Wait for server and dynamic front to be ready. Run this at the beginning of every integration test

new_client()

Reset user agent

return value

Return a user agent

create_user($ua, $args_ref)

Call API to create a user

Arguments

$ua - user agent

$args_ref - fields

edit_user($ua, $args_ref)

Call API to edit a user, see create_user

login($ua, $user_id, $password)

Login as a user

get_page ($ua, $url)

Get a page of the app

Arguments

$ua - user agent

$url - absolute url

post_form ($ua, $url, $fields_ref)

Post a form

Arguments

$ua - user agent

$url - absolute url

$fields_ref

Reference of a hash of fields to pass as the form result

edit_product($ua, $product_fields_ref)

Call the API to edit a product. If the product does not exist, it will be created.

Arguments

$ua - user agent

$product_fields_ref

Reference of a hash of product fields to pass to the API

html_displays_error($page)

Return if a form displays errors

Most forms will return a 200 while displaying an error message. This function assumes error_list.tt.html was used.

construct_test_url()

Constructs the URL to send the HTTP request to for the API.

Arguments

Takes in two string arguments, One being the the target and other a prefix. The prefix could be simply the country code (eg: US for America or "World") OR something like ( {country-code}-{language-code} )

An example below $target = "/product/35242200055" $prefix= "world-fr"

Return Value

Returns the constructed URL for the query

For the example cited above this returns: "http://world-fr.openfoodfacts.localhost/product/35242200055"

origin_from_url($url)

Compute "Origin" header for $url

execute_api_tests($file, $tests_ref, $ua=undef)

Initialize tests and execute them.

Arguments

$file test file name

The *.t test files call execute_api_tests() with _FILE_ as the first parameter, and the directories for the tests are derived from it.

$tests_ref reference to list of tests

The tests are in a structure like this:

my $tests_ref = ( [ { # request description setup => 1, # optional, if set to 1, the request will be executed (e.g. to create a product to test on) but the result content will not be checked # expected status code (defaulting to 200) will still be checked to report setup issues test_case => 'no-body', # test case id, must be unique as it is used to name the expected results file method => 'POST', # defaults to GET subdomain => 'world', # defaults to "world" path => '/api/v3/product/12345678', query_string => '?some_param=some_value&some_other_param=some_other_value' # optional form => { field_name => field_value, .. }, # optional, will not be sent if there is a body headers_in => {header1 => value1}, # optional, headers to add to request body => '{"some_json_field": "some_value"}', # optional, will be fetched in file in needed ua => a LWP::UserAgent object, if a specific user is needed (e.g. with moderator status)

                        # expected return
                        expected_status_code => 200,    # optional. Defaults to 200
                        headers => {header1 => value1, }  # optional. You may add an undef value to test for the inexistance of a header
                        response_content_must_match => "regexp" # optional. You may add a case insensitive regexp (e.g. "Product saved") that must be matched
                        response_content_must_not_match => "regexp"     # optional. You may add a case insensitive regexp (e.g. "error") that must not be matched
                        sort_products_by => "product_name" # optional. You may provide a field to sort the returned products by so that they are in an expected order
                }
    ],
);

$ua a web client (LWP::UserAgent) to use

If undef we open a new client.

You might need this to test with an authenticated user.

Note: this setting can be overriden for each test case by specifying a "ua" field.

tail_log_start($log_path)

Start monitoring a log file

Arguments

String $log_path

Defaults to /var/log/apache2/log4perl.log

Returns

An object to pass to tail_log_read to read

tail_log_read($tail)

Return all content written to a log file since last check

Arguments

$tail

Object returned by tail_log_start

Returns

Content as a string

mails_from_log($text)

Retrieve mails in a log extract

mail_to_text($text)

Make mail more easy to search by removing some specific formatting

Especially we replace "3D=" for "=" and join line and their continuation

Arguments

$mail text of mail

Returns

Reformatted text

normalize_mail_for_comparison($mail)

Replace parts of mail that varies from tests to tests, and also in a format that's nice in json.

Arguments

$mail text of mail

Returns

ref to an array of lines of the email

fake_http_server($port, $dump_path, $responses_ref) {

Launch a fake HTTP server.

We use that to simulate Robotoff or any HTTP API in integration tests. As it will be launched on the local backend container, we have to pretend those service URL is on backend:$port.

You can provide a list of responses to simulate real service responses, while requests sent are store for later checks by the tests.

parameters

$dump_path - path

A temporary directory to dump requests

You can retrieve requests, in this directory as req-n.sto

$responses_ref - ref to a list

List of responses to send, in right order, for each received request.

If the number of request exceed this list, we will send simple 200 HTTP responses with a json payload.

returns ref to fake server

Hold the reference until you don't need the server

get_minion_jobs($task_name, $created_after_ts, $max_waiting_time)

Subprogram which wait till the minion finished its job or if it takes too much time

Arguments

$task_name

The name of the task

$created_after_ts

The timestamp of the creation of the task

$max_waiting_time

The max waiting time for this given task

Returns

Returns a list of jobs information associated with the task_name

Note: for each job we return the job information (as returned by the jobs() iterator), not the Minion job object.

<<