def test_stats_checks(client, request_users, test_name, request, snapshot_stack, path): """Tests the /xhr/stats/checks/ endpoint's context.""" user = request_users["user"] # FIXME: stats refreshing boilerplate global users_with_stats if user not in users_with_stats: request.getfixturevalue("refresh_stats") users_with_stats.add(user) with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(path)]): if not user.is_anonymous: client.force_login(user) url = "/xhr/stats/checks/?path=%s" % path response = client.get(url, HTTP_X_REQUESTED_WITH="XMLHttpRequest") with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("response_body") as snapshot: snapshot.assert_matches(response.json())
def test_duedate_delete(client, request_users, test_name, snapshot_stack, path): """Tests removing existing due dates.""" user = request_users["user"] data = { "due_on": timezone.now(), "modified_by": user, "pootle_path": path, } due_date = DueDateFactory.create(**data) assert due_date.id is not None url = "/xhr/duedates/%s/" % due_date.id with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(path)]): if not user.is_anonymous: client.force_login(user) response = client.delete(url, content_type="application/json") with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.json())
def test_duedate_delete(client, request_users, test_name, snapshot_stack, path): """Tests removing existing due dates.""" user = request_users['user'] data = { 'due_on': datetime.now(), 'modified_by': user, 'pootle_path': path, } due_date = DueDateFactory.create(**data) assert due_date.id is not None url = '/xhr/duedates/%s/' % due_date.id with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(path)]): if not user.is_anonymous: client.force_login(user) response = client.delete(url, content_type='application/json') with snapshot_stack.push('status_code') as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push('context') as snapshot: snapshot.assert_matches(response.json())
def test_export( client, request_users, test_name, monkeypatch, snapshot_stack, url, limit, include_disabled, ): """Tests correctness of the export view context.""" user = request_users["user"] stack = [as_dir(test_name), as_dir(user.username), url_name(url)] if limit is not None: stack.append("limit=%s" % limit) monkeypatch.setattr("pootle.core.views.export.UNITS_LIMIT", limit) if include_disabled: stack.append("include_disabled") url += "?all" with snapshot_stack.push(stack): if not user.is_anonymous: client.force_login(user) response = client.get(url) with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.context)
def test_get_stats(client, request_users, test_name, request, snapshot_stack, params): """Tests the /xhr/stats/ view.""" url = "/xhr/stats/" user = request_users["user"] # stats refreshing boilerplate global users_with_stats if user not in users_with_stats: request.getfixturevalue("refresh_stats") users_with_stats.add(user) qs = urllib.parse.urlencode(params, True) with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(url + qs)]): if not user.is_anonymous: client.force_login(user) response = client.get(url, params, HTTP_X_REQUESTED_WITH="XMLHttpRequest") with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("response") as snapshot: snapshot.assert_matches(response.json())
def test_get_tasks_permissions( client, test_name, snapshot_stack, refresh_stats, request, request_users, patch_timezone_now, project0, hide, ): """Tests pending tasks retrieval with restricted permissions.""" language = "language0" url = "/xhr/tasks/%s/" % language user = request_users["user"] # Disallow `project0` access to `member` if user.username == "member": _require_permission_set(user, project0.directory, negative_permissions=[hide]) # FIXME: stats refreshing boilerplate global users_with_stats if user not in users_with_stats: request.getfixturevalue("refresh_stats") users_with_stats.add(user) now = aware_datetime(2017, 2, 28, 9, 0) patch_timezone_now(now) DueDateFactory.create( pootle_path="/projects/project0/", due_on=now + timedelta(days=8), ) DueDateFactory.create( pootle_path="/%s/project0/store0.po" % language, due_on=now + timedelta(days=2), ) DueDateFactory.create( pootle_path="/%s/project1/store0.po" % language, due_on=now + timedelta(days=4), ) with snapshot_stack.push([as_dir(test_name), as_dir(user.username)]): if not user.is_anonymous: client.force_login(user) response = client.get(url) with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.json())
def test_get_tasks_invalid_language(client, request_users, test_name, snapshot_stack): """Tests attempting to retrieve tasks for an invalid language.""" url = "/xhr/tasks/invalid-language-code/" user = request_users["user"] with snapshot_stack.push([as_dir(test_name), as_dir(user.username)]): if not user.is_anonymous: client.force_login(user) response = client.get(url) with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.json())
def test_taskresultset_importance_order(test_name, snapshot_stack, sort_type, fake_task_args): """Tests ordering of tasks according to their importance.""" tasks = [LightTask(*task_args) for task_args in fake_task_args] with snapshot_stack.push([as_dir(test_name), sort_type]) as snapshot: task_resultset = TaskResultSet(tasks).order_by_importance() snapshot.assert_matches([task for task in task_resultset])
def test_translate(client, request_users, test_name, snapshot_stack, url): """Tests correctness of the translate view context.""" user = request_users["user"] with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(url)]): if not user.is_anonymous: client.force_login(user) response = client.get(url) with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.context)
def test_export(client, request_users, test_name, monkeypatch, snapshot_stack, url, limit): """Tests correctness of the export view context.""" user = request_users['user'] stack = [as_dir(test_name), as_dir(user.username), url_name(url)] if limit is not None: stack.append('limit=%s' % limit) monkeypatch.setattr('pootle.core.views.export.UNITS_LIMIT', limit) with snapshot_stack.push(stack): if not user.is_anonymous: client.force_login(user) response = client.get(url) with snapshot_stack.push('status_code') as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push('context') as snapshot: snapshot.assert_matches(response.context)
def test_browse(client, request_users, test_name, request, snapshot_stack, url): """Tests correctness of the browsing view context.""" user = request_users["user"] # stats refreshing boilerplate global users_with_stats if user not in users_with_stats: request.getfixturevalue("refresh_stats") users_with_stats.add(user) with snapshot_stack.push([as_dir(test_name), as_dir(user.username), url_name(url)]): if not user.is_anonymous: client.force_login(user) response = client.get(url) with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.context)
def test_get_tasks(client, request, request_users, test_name, snapshot_stack, patch_timezone_now): """Tests pending tasks retrieval.""" language = 'language0' url = '/xhr/tasks/%s/' % language user = request_users['user'] # FIXME: stats refreshing boilerplate global users_with_stats if user not in users_with_stats: request.getfixturevalue('refresh_stats') users_with_stats.add(user) now = aware_datetime(2017, 02, 28, 9, 00) patch_timezone_now(now) DueDateFactory.create( pootle_path='/projects/project0/', due_on=now + timedelta(days=8), ) DueDateFactory.create( pootle_path='/%s/project0/store0.po' % language, due_on=now + timedelta(days=2), ) DueDateFactory.create( pootle_path='/%s/project1/store0.po' % language, due_on=now + timedelta(days=4), ) with snapshot_stack.push([as_dir(test_name), as_dir(user.username)]): if not user.is_anonymous: client.force_login(user) response = client.get(url) with snapshot_stack.push('status_code') as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push('context') as snapshot: snapshot.assert_matches(response.json())
def test_duedate_update(client, request_users, test_name, snapshot_stack, path): """Tests updating existing due dates.""" user = request_users["user"] initial_due_on = aware_datetime(2017, 1, 26, 1, 2, 3) data = { "due_on": initial_due_on, "modified_by": user, "pootle_path": path, } due_date = DueDateFactory.create(**data) assert due_date.id is not None url = "/xhr/duedates/%s/" % due_date.id put_data = { "due_on": "2017-01-27", "pootle_path": path, } with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(path)]): if not user.is_anonymous: client.force_login(user) response = client.put(url, jsonify(put_data), content_type="application/json") if response.status_code == 200: due_date.refresh_from_db() assert due_date.due_on != initial_due_on with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.json())
def test_duedate_update(client, request_users, test_name, snapshot_stack, path): """Tests updating existing due dates.""" user = request_users['user'] initial_due_on = aware_datetime(2017, 01, 26, 01, 02, 03) data = { 'due_on': initial_due_on, 'modified_by': user, 'pootle_path': path, } due_date = DueDateFactory.create(**data) assert due_date.id is not None url = '/xhr/duedates/%s/' % due_date.id put_data = { 'due_on': '2017-01-27', 'pootle_path': path, } with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(path)]): if not user.is_anonymous: client.force_login(user) response = client.put(url, jsonify(put_data), content_type='application/json') if response.status_code == 200: due_date.refresh_from_db() assert due_date.due_on != initial_due_on with snapshot_stack.push('status_code') as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push('context') as snapshot: snapshot.assert_matches(response.json())
def test_duedate_post(client, request_users, test_name, snapshot_stack, path): """Tests creating new due dates.""" url = "/xhr/duedates/" user = request_users["user"] post_data = { "due_on": "2017-01-26", "pootle_path": path, } with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(path)]): if not user.is_anonymous: client.force_login(user) response = client.post(url, jsonify(post_data), content_type="application/json") with snapshot_stack.push("status_code") as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push("context") as snapshot: snapshot.assert_matches(response.json())
def test_duedate_post(client, request_users, test_name, snapshot_stack, path): """Tests creating new due dates.""" url = '/xhr/duedates/' user = request_users['user'] post_data = { 'due_on': '2017-01-26', 'pootle_path': path, } with snapshot_stack.push( [as_dir(test_name), as_dir(user.username), url_name(path)]): if not user.is_anonymous: client.force_login(user) response = client.post(url, jsonify(post_data), content_type='application/json') with snapshot_stack.push('status_code') as snapshot: snapshot.assert_matches(response.status_code) with snapshot_stack.push('context') as snapshot: snapshot.assert_matches(response.json())
def test_stats_retrieval_no_stats(test_name, snapshot_stack, flush_stats): """Tests no stats are provided when these haven't been calculated.""" path = '/language0/project0/' with snapshot_stack.push([as_dir(test_name), url_name(path)]) as snapshot: stats = Stats(path) stats_data = { 'total': stats.total, 'translated': stats.translated, 'fuzzy': stats.fuzzy, 'incomplete': stats.incomplete, 'critical': stats.critical, 'suggestions': stats.suggestions, 'last_action': stats.last_action, 'last_updated': stats.last_updated, } snapshot.assert_matches(stats_data)
def test_stats_retrieval_dummy(test_name, snapshot_stack, refresh_stats): """Tests no stats are provided for dummy paths.""" path = '/non/existing/' with snapshot_stack.push([as_dir(test_name), url_name(path)]) as snapshot: stats = Stats(path) stats_data = { 'total': stats.total, 'translated': stats.translated, 'fuzzy': stats.fuzzy, 'incomplete': stats.incomplete, 'critical': stats.critical, 'suggestions': stats.suggestions, 'last_action': stats.last_action, 'last_updated': stats.last_updated, } snapshot.assert_matches(stats_data)
def test_stats_retrieval(test_name, snapshot_stack, refresh_stats): """Tests stats values are retrieved properly.""" path = '/language0/project0/' with snapshot_stack.push([as_dir(test_name), url_name(path)]) as snapshot: stats = Stats(path) stats_data = { 'total': stats.total, 'translated': stats.translated, 'fuzzy': stats.fuzzy, 'incomplete': stats.incomplete, 'critical': stats.critical, 'suggestions': stats.suggestions, 'last_action': stats.last_action, 'last_updated': stats.last_updated, } snapshot.assert_matches(stats_data)
def test_stats_retrieval_no_stats(test_name, snapshot_stack, flush_stats): """Tests no stats are provided when these haven't been calculated.""" path = "/language0/project0/" with snapshot_stack.push([as_dir(test_name), url_name(path)]) as snapshot: stats = Stats(path) stats_data = { "total": stats.total, "translated": stats.translated, "fuzzy": stats.fuzzy, "incomplete": stats.incomplete, "critical": stats.critical, "suggestions": stats.suggestions, "last_action": stats.last_action, "last_updated": stats.last_updated, } snapshot.assert_matches(stats_data)
def test_stats_retrieval_dummy(test_name, snapshot_stack, refresh_stats): """Tests no stats are provided for dummy paths.""" path = "/non/existing/" with snapshot_stack.push([as_dir(test_name), url_name(path)]) as snapshot: stats = Stats(path) stats_data = { "total": stats.total, "translated": stats.translated, "fuzzy": stats.fuzzy, "incomplete": stats.incomplete, "critical": stats.critical, "suggestions": stats.suggestions, "last_action": stats.last_action, "last_updated": stats.last_updated, } snapshot.assert_matches(stats_data)
def test_stats_retrieval(test_name, snapshot_stack, refresh_stats): """Tests stats values are retrieved properly.""" path = "/language0/project0/" with snapshot_stack.push([as_dir(test_name), url_name(path)]) as snapshot: stats = Stats(path) stats_data = { "total": stats.total, "translated": stats.translated, "fuzzy": stats.fuzzy, "incomplete": stats.incomplete, "critical": stats.critical, "suggestions": stats.suggestions, "last_action": stats.last_action, "last_updated": stats.last_updated, } snapshot.assert_matches(stats_data)