Module tests
Contents
Module tests
#
Utils#
Module with various useful functions for tests.
- tests.utils.get_form_errors(text: str) list #
- Returns
errors displayed to users by a form when it is not validated.
- tests.utils.load_data_from_form(text: str, form_id: str) dict #
From an html form, return a dictionnary with its data.
Ex:
data = utils.load_data_from_form(response.text, "new_price")
Then
data
can be modified and used as data for a POST request:data["status"] = int(EventStatus.Cancelled) response = client.post(f"/collectives/{event.id}/edit", data=data)
- Parameters
text (string) – Raw page HTML to extract form from
form_id (string) – the html id of the form to extract
- Returns
the content of the form as a dict
Fixtures#
Module for all fixtures.
App#
Creation of fixture app
- tests.fixtures.app.app(db_file)#
Session-wide test Flask application.
- tests.fixtures.app.db_file()#
Generate a file path for a db file, and delete it after use
- tests.fixtures.app.enable_sanctions()#
Enable sanctions in configuration
Events#
Module to create fixture events.
- tests.fixtures.event.activity_event(prototype_activity_event)#
- Returns
An event in with “Activity” visibility level
- tests.fixtures.event.cancelled_event(prototype_cancelled_event)#
- Returns
A cancelled event
- tests.fixtures.event.disabled_paying_event(prototype_disabled_paying_event)#
- Returns
An event in draft status
- tests.fixtures.event.draft_event(prototype_draft_event)#
- Returns
An event in draft status
- tests.fixtures.event.event1_with_answers(event1_with_questions)#
- Returns
An event with answered questions
- tests.fixtures.event.event1_with_questions(event1_with_reg)#
- Returns
An event with user registrations and associated questions
- tests.fixtures.event.event1_with_reg(event1, user1, user2, user3, user4)#
- Returns
The fixture event1, with 4 users registered.
- Return type
- tests.fixtures.event.event1_with_reg_waiting_list(event1, user1, user2, user3, user4)#
- Returns
The fixture event1, with 4 users registered, and 2 in waiting_list.
- Return type
- tests.fixtures.event.event_in_less_than_x_hours(prototype_event_in_less_than_x_hours)#
Fixture for an event starting in less than 48 hours (parameterized).
- tests.fixtures.event.event_in_less_than_x_hours_with_reg(prototype_event_in_less_than_x_hours, user_with_no_warning_badge, user_with_valid_first_warning_badge, user_with_expired_first_warning_badge, user_with_valid_second_warning_badge, user_with_expired_second_warning_badge, user_with_expired_suspended_badge)#
Returns an event in less than 48 hours (parameterized) with registrations for specified users.
- tests.fixtures.event.event_with_no_activity_type_in_less_than_x_hours_with_reg(prototype_event_in_less_than_x_hours, user_with_no_warning_badge, user_with_valid_first_warning_badge)#
Returns an event in less than 48 hours (parameterized) with registrations for specified users.
- tests.fixtures.event.free_paying_event(prototype_free_paying_event)#
- Returns
An event in draft status
- tests.fixtures.event.generate_event(identifier)#
Creates a new fixture event.
- Parameters
identifier (string) – Any string. It will be add to event title
- tests.fixtures.event.inject_fixture(name, identifier)#
Inject a new event into fixtures.
- Parameters
name (string) – Fixture name
identifier (string) – Any string. It will be add to event title
- tests.fixtures.event.past_event(prototype_past_event)#
- Returns
A past event
- tests.fixtures.event.paying_event(prototype_paying_event)#
- Returns
An event with associated payment_item and prices
- tests.fixtures.event.tagged_event(prototype_tagged_event)#
- Returns
A tagged event as Handicaf.
- tests.fixtures.event.youth_event(prototype_youth_event: collectives.models.event.Event)#
- Returns
An event in with registration restricted to youths
Client#
Module to create flask client from existing fixture users.
Connection to the application does not use http but a special client available from the app object. See flask documentation.
This client is used to test GET and POST requests. Base client fixture is
client()
For some requests, login is required. Thus, some basic fixtures are offered regarding
the client role that will be tested. EG: admin_client()
or user1_client()
.
Those client are based on client()
.
Warning
Please note that Flask app offer ONLY ONE CLIENT. Thus, if you need to switch between
roles in the same test, please use only one client fixture and change user using
login()
- tests.fixtures.client.admin_client(client, admin_user, app)#
Flask client authenticated as admin.
- tests.fixtures.client.client(app)#
Raw and unauthenticated flask client.
- tests.fixtures.client.client_with_expired_benevole_badge(client, user_with_expired_benevole_badge)#
Flask client authenticated as user with an expired badge.
- tests.fixtures.client.client_with_expired_first_warning_badge(client, user_with_expired_first_warning_badge)#
Flask client authenticated as user with an expired first warning badge.
- tests.fixtures.client.client_with_expired_second_warning_badge(client, user_with_expired_second_warning_badge)#
Flask client authenticated as user with an expired second warning badge.
- tests.fixtures.client.client_with_expired_suspended_badge(client, user_with_expired_suspended_badge)#
Flask client authenticated as user with an expired suspended badge.
- tests.fixtures.client.client_with_no_warning_badge(client, user_with_no_warning_badge)#
Flask client authenticated as user with no late unregistration-related warning badge.
- tests.fixtures.client.client_with_valid_benevole_badge(client, user_with_valid_benevole_badge)#
Flask client authenticated as user with a valid badge.
- tests.fixtures.client.client_with_valid_first_warning_badge(client, user_with_valid_first_warning_badge)#
Flask client authenticated as user with a valid first warning badge.
- tests.fixtures.client.client_with_valid_second_warning_badge(client, user_with_valid_second_warning_badge)#
Flask client authenticated as user with a valid second warning badge.
- tests.fixtures.client.client_with_valid_suspended_badge(client, user_with_valid_suspended_badge)#
Flask client authenticated as user with a valid suspended badge.
- tests.fixtures.client.extranet_user_client(client, extranet_user)#
Flask client authenticated as extranet_user user.
- tests.fixtures.client.hotline_client(client, hotline_user)#
Flask client authenticated as hotline.
- tests.fixtures.client.leader_client(client, leader_user)#
Flask client authenticated as regular user.
- tests.fixtures.client.login(client, user, password='fooBar2+!', deactivate_spam_protection=True)#
Log the client with given user.
If client has already a connected user, the user will be silently logout.
- Parameters
client – The test client from the flask app.
user (User) – the user that will be login
password (string) – The password to use to login user. Default: see
tests.fixtures.user.PASSWORD
deactivate_spam_protection (bool) – allow to do as much connexion as wanted
- Returns
True if login is succesfull, False if not.
- tests.fixtures.client.logout(client)#
Logs out a client
- Parameters
client – The flask app test_client.
- Returns
True if logout is succesfull
- Return type
bool
- tests.fixtures.client.supervisor_client(client, supervisor_user)#
Flask client authenticated as Alpinisme activity supervisor.
- tests.fixtures.client.user1_client(client, user1)#
Flask client authenticated as regular user.
- tests.fixtures.client.user3_client(client, user3)#
Flask client authenticated as regular user.
- tests.fixtures.client.youth_client(client, youth_user)#
Flask client authenticated as youth user.
User#
Module to create fixture users.
- tests.fixtures.user.PASSWORD = 'fooBar2+!'#
Default test password for non admin users.
- Type
string
- tests.fixtures.user.USER_NAMES = [('Jan', 'Johnston'), ('Evan', 'Walsh'), ('Kimberly', 'Paterson'), ('Jake', 'Marshall'), ('Boris', 'Bailey'), ('Frank', 'Morgan'), ('Chloe', 'White'), ('Michael', 'Davidson'), ('Theresa', 'Bailey'), ('Jake', 'Piper')]#
Basic list of names.
- Type
list()
- tests.fixtures.user.add_badge_to_user(user, badge_id, level=1, expiration_date=datetime.date(2025, 12, 22), activity_name='Alpinisme')#
Manage to add a badge to a user
- Parameters
user (User) – the user to add a badge to
badge_id (badgeIds) – the type of badge to add
activity_name (string) – The activity name for the role. Default Alpinisme
- Expiration_date
the expiration date of the badge (Default is today + 1 year, so a valid one)
- tests.fixtures.user.add_benevole_badge_to_user(user, expiration_date=datetime.date(2025, 12, 22), badge_id=1, activity_name='Alpinisme')#
Manage to add a badge to a user
- Parameters
user (User) – the user to add a badge to
badge_id (badgeIds) – the type of badge to add
activity_name (string) – The activity name for the role. Default Alpinisme
- Expiration_date
the expiration date of the badge (Default is today + 1 year, so a valid one)
- tests.fixtures.user.admin_user(app)#
- Returns
The admin user.
- tests.fixtures.user.extranet_user(prototype_extranet_user)#
- Returns
A user with type extranet.
- tests.fixtures.user.generate_user(names)#
Generate fixture users.
- Parameters
id (int) – An identifiying integer for the user
names ((string,string)) – first and last names for the user
- Returns
the newly created user
- Return type
- tests.fixtures.user.hotline_user(prototype_hotline_user)#
- Returns
A user with a hotline role.
- tests.fixtures.user.inject_fixture(name, names)#
Create and add a new fixture user.
- Parameters
name (string) – Fixture name.
names ("(string,string)") – First and lastname of the user.
- tests.fixtures.user.leader2_user(prototype_leader2_user)#
- Returns
An Alpinisme leader user.
- tests.fixtures.user.leader2_user_with_event(leader2_user, event2)#
- Returns
A leader User which leads event2
- tests.fixtures.user.leader_user(prototype_leader_user)#
- Returns
An Alpinisme leader user.
- tests.fixtures.user.leader_user_with_event(leader_user, event1)#
- Returns
A leader User which leads event1
- tests.fixtures.user.president_user(prototype_president_user)#
- Returns
A user with a president role.
- tests.fixtures.user.promote_to_leader(user, activity='Alpinisme')#
Add a leader role to a user.
- Parameters
user (User) – the user to be promoted
activity (string) – Activity name to which user will be promoted. Defaul Alpinisme
- tests.fixtures.user.promote_user(user, role_id, activity_name='Alpinisme', confidentiality_agreement_signature=True)#
Manage to add a role to a user
- tests.fixtures.user.supervisor_user(prototype_supervisor_user)#
- Returns
A user with an Alpinisme supervisor role.
- tests.fixtures.user.user_with_expired_benevole_badge(prototype_user_with_expired_benevole_badge: collectives.models.user.User)#
- Returns
A user with an expired benevole Badge.
- tests.fixtures.user.user_with_expired_first_warning_badge(prototype_user_with_expired_first_warning_badge: collectives.models.user.User, enable_sanctions)#
- Returns
A user with an expired first warning Badge.
- tests.fixtures.user.user_with_expired_second_warning_badge(prototype_user_with_expired_second_warning_badge: collectives.models.user.User, enable_sanctions)#
- Returns
A user with an expired second warning Badge.
- tests.fixtures.user.user_with_expired_suspended_badge(prototype_user_with_expired_suspended_badge: collectives.models.user.User, enable_sanctions)#
- Returns
A user with an expired suspended Badge.
- tests.fixtures.user.user_with_valid_benevole_badge(prototype_user_with_valid_benevole_badge: collectives.models.user.User)#
- Returns
A user with a valid benevole Badge.
- tests.fixtures.user.user_with_valid_first_warning_badge(prototype_user_with_valid_first_warning_badge: collectives.models.user.User, enable_sanctions)#
- Returns
A user with a valid first warning Badge.
- tests.fixtures.user.user_with_valid_second_warning_badge(prototype_user_with_valid_second_warning_badge: collectives.models.user.User, enable_sanctions)#
- Returns
A user with a valid second warning Badge.
- tests.fixtures.user.user_with_valid_suspended_badge(prototype_user_with_valid_suspended_badge: collectives.models.user.User, enable_sanctions)#
- Returns
A user with a valid suspended Badge.
- tests.fixtures.user.youth_user(prototype_youth_user: collectives.models.user.User)#
- Returns
A user with a youth license.
Mocking ressources#
Mock module for better tests.#
Mocks are build to test without the need of outside ressources such as extranet ou payment website. To mock it, monkeypatch (see doc <https://docs.pytest.org/en/7.1.x/how-to/monkeypatch.html>_) is used. It exchanges a function, a method or a class by another one from this module.
Unit tests#
Unit tests modules#
Basic unit tests for an application module. This module architecture mimics the
collectives
one, eg, collectives.utils.test_extranet
functions
are tested by tests.unit.utils.test_extranet
.