def test_push_event_detected(client): project = f.ProjectFactory() url = reverse("github-hook-list") url = "%s?project=%s" % (url, project.id) data = {"commits": [ {"message": "test message"}, ]} GitHubViewSet._validate_signature = mock.Mock(return_value=True) with mock.patch.object(event_hooks.PushEventHook, "process_event") as process_event_mock: response = client.post(url, json.dumps(data), HTTP_X_GITHUB_EVENT="push", content_type="application/json") assert process_event_mock.call_count == 1 assert response.status_code == 204
def test_create_member_using_username_without_being_contacts(client): project = f.ProjectFactory() john = f.UserFactory.create() joseph = f.UserFactory.create() tester = f.RoleFactory(project=project, name="Tester") f.MembershipFactory(project=project, user=john, is_admin=True) url = reverse("memberships-list") data = { "project": project.id, "role": tester.pk, "username": joseph.username } client.login(john) response = client.json.post(url, json.dumps(data)) assert response.status_code == 400 assert "The user must be a valid contact" in response.data["username"][0]
def test_not_ip_filter(client): project = f.ProjectFactory() f.ProjectModulesConfigFactory( project=project, config={"gitlab": { "secret": "tpnIwJDz4e", "valid_origin_ips": [], }}) url = reverse("gitlab-hook-list") url = "{}?project={}&key={}".format(url, project.id, "tpnIwJDz4e") data = {"test:": "data"} response = client.post(url, json.dumps(data), content_type="application/json", REMOTE_ADDR="111.111.111.111") assert response.status_code == 204
def test_create_issue_in_milestone(client): user = f.UserFactory.create() project = f.ProjectFactory.create(owner=user, default_task_status=None) project.default_issue_status = f.IssueStatusFactory.create(project=project) f.MembershipFactory.create(project=project, user=user, is_admin=True) milestone = f.MilestoneFactory(project=project) project.save() url = reverse("issues-list") data = {"subject": "Test issue with milestone", "project": project.id, "milestone": milestone.id} client.login(user) response = client.json.post(url, json.dumps(data)) assert response.status_code == 201 assert response.data['status'] == project.default_issue_status.id assert response.data['project'] == project.id assert response.data['milestone'] == milestone.id
def test_set_related_userstory_existing(client): user = f.UserFactory.create() epic = f.EpicFactory.create() us = f.UserStoryFactory.create() related_us = f.RelatedUserStory.create(epic=epic, user_story=us, order=55) f.MembershipFactory.create(project=epic.project, user=user, is_admin=True) f.MembershipFactory.create(project=us.project, user=user, is_admin=True) url = reverse('epics-related-userstories-detail', args=[epic.pk, us.pk]) data = { "order": 77 } client.login(user) response = client.json.patch(url, json.dumps(data)) assert response.status_code == 200 related_us.refresh_from_db() assert related_us.order == 77
def test_issue_custom_attribute_duplicate_name_error_on_move_between_projects( client): custom_attr_1 = f.IssueCustomAttributeFactory() custom_attr_2 = f.IssueCustomAttributeFactory(name=custom_attr_1.name) member = f.MembershipFactory(user=custom_attr_1.project.owner, project=custom_attr_1.project, is_admin=True) f.MembershipFactory(user=custom_attr_1.project.owner, project=custom_attr_2.project, is_admin=True) url = reverse("issue-custom-attributes-detail", kwargs={"pk": custom_attr_2.pk}) data = {"project": custom_attr_1.project.pk} client.login(member.user) response = client.json.patch(url, json.dumps(data)) assert response.status_code == 400
def test_create_comment_disabled(client): user = f.UserFactory.create() project = f.ProjectFactory.create() project.is_contact_activated = False project.save() f.MembershipFactory(user=project.owner, project=project, is_admin=True) url = reverse("contact-list") contact_data = json.dumps({ "project": project.id, "comment": "Testing comment" }) client.login(user) response = client.post(url, contact_data, content_type="application/json") assert response.status_code == 403
def test_update_user_with_allowed_domain_email(client, settings): settings.USER_EMAIL_ALLOWED_DOMAINS = ['email.com'] user = f.UserFactory.create(email="*****@*****.**") url = reverse('users-detail', kwargs={"pk": user.pk}) data = {"email": "*****@*****.**"} client.login(user) response = client.patch(url, json.dumps(data), content_type="application/json") assert response.status_code == 200 user.refresh_from_db() assert user.email == "*****@*****.**" assert user.email_token is not None assert user.new_email == "*****@*****.**"
def test_create_issue_without_status_in_project_without_default_values(client): user = f.UserFactory.create() project = f.ProjectFactory.create(owner=user, default_issue_status=None, default_priority=None, default_severity=None, default_issue_type = None) f.MembershipFactory.create(project=project, user=user, is_admin=True) url = reverse("issues-list") data = {"subject": "Test user story", "project": project.id} client.login(user) response = client.json.post(url, json.dumps(data)) assert response.status_code == 201 assert response.data['status'] == None assert response.data['severity'] == None assert response.data['priority'] == None assert response.data['type'] == None
def test_api_create_membership_without_enough_memberships_public_project_slots_one_projects( client): user = f.UserFactory.create(max_memberships_public_projects=1) project = f.ProjectFactory(owner=user, is_private=False) role = f.RoleFactory(project=project, name="Test") f.MembershipFactory(project=project, user=user, is_admin=True) client.login(user) url = reverse("memberships-list") data = { "role": role.pk, "project": project.pk, "username": "******" } response = client.json.post(url, json.dumps(data)) assert response.status_code == 400 assert "reached your current limit of memberships for public" in response.data[ "_error_message"]
def test_update_milestone_with_userstories_list(client): user = f.UserFactory.create() project = f.ProjectFactory.create(owner=user) role = f.RoleFactory.create(project=project) f.MembershipFactory.create(project=project, user=user, role=role, is_admin=True) sprint = f.MilestoneFactory.create(project=project, owner=user) f.PointsFactory.create(project=project, value=None) us = f.UserStoryFactory.create(project=project, owner=user) url = reverse("milestones-detail", args=[sprint.pk]) form_data = {"name": "test", "user_stories": [{"id": us.id}]} client.login(user) response = client.json.patch(url, json.dumps(form_data)) assert response.status_code == 200
def test_issue_resource_history_test(client): user = f.UserFactory.create() project = f.ProjectFactory.create(owner=user) role = f.RoleFactory.create(project=project) f.MembershipFactory.create(project=project, user=user, role=role, is_admin=True) issue = f.IssueFactory.create(owner=user, project=project) mock_path = "tuesmon.projects.issues.api.IssueViewSet.pre_conditions_on_save" url = reverse("issues-detail", args=[issue.pk]) client.login(user) qs_all = HistoryEntry.objects.all() qs_deleted = qs_all.filter(type=HistoryType.delete) qs_changed = qs_all.filter(type=HistoryType.change) qs_created = qs_all.filter(type=HistoryType.create) assert qs_all.count() == 0 with patch(mock_path): data = {"subject": "Fooooo", "version": issue.version} response = client.patch(url, json.dumps(data), content_type="application/json") assert response.status_code == 200 assert qs_all.count() == 1 assert qs_created.count() == 1 assert qs_changed.count() == 0 assert qs_deleted.count() == 0 with patch(mock_path): response = client.delete(url) assert response.status_code == 204 assert qs_all.count() == 2 assert qs_created.count() == 1 assert qs_changed.count() == 0 assert qs_deleted.count() == 1
def test_api_create_new_user_story_with_tag_capitalized(client): project = f.ProjectFactory.create() f.MembershipFactory.create(project=project, user=project.owner, is_admin=True) url = reverse("userstories-list") data = { "subject": "Test user story", "project": project.id, "tags": [ ["CapTag", "#fabada"] ] } client.login(project.owner) response = client.json.post(url, json.dumps(data)) assert response.status_code == 201, response.data project.refresh_from_db() assert project.tags_colors == [["captag", "#fabada"]]
def test_user_story_action_bulk_update_milestone(client, data): url = reverse('userstories-bulk-update-milestone') users = [ None, data.registered_user, data.project_member_without_perms, data.project_member_with_perms, data.project_owner ] post_data = json.dumps({ "bulk_stories": [{ "us_id": data.private_user_story1.id, "order": 2 }], "milestone_id": data.private_sprint1.pk, "project_id": data.private_project1.pk }) results = helper_test_http_method(client, 'post', url, post_data, users) assert results == [401, 403, 403, 204, 204]
def test_api_patch_project_modules(client): project = f.create_project() f.MembershipFactory(project=project, user=project.owner, is_admin=True) url = reverse("projects-modules", args=(project.id,)) client.login(project.owner) data = { "github": { "secret": "test_secret", "url": "test_url", } } response = client.patch(url, json.dumps(data), content_type="application/json") assert response.status_code == 204 config = services.get_modules_config(project).config assert "github" in config assert config["github"]["secret"] == "test_secret" assert config["github"]["webhooks_url"] != "test_url"
def test_api_create_bulk_members(client): project = f.ProjectFactory() john = f.UserFactory.create() joseph = f.UserFactory.create() other = f.UserFactory.create() tester = f.RoleFactory(project=project, name="Tester", permissions=["view_project"]) gamer = f.RoleFactory(project=project, name="Gamer", permissions=["view_project"]) f.MembershipFactory(project=project, user=john, role=tester, is_admin=True) # John and Other are members from another project project2 = f.ProjectFactory() f.MembershipFactory(project=project2, user=john, role=gamer, is_admin=True) f.MembershipFactory(project=project2, user=other, role=gamer) url = reverse("memberships-bulk-create") data = { "project_id": project.id, "bulk_memberships": [ { "role_id": gamer.pk, "username": joseph.email }, { "role_id": gamer.pk, "username": other.username }, ] } client.login(john) response = client.json.post(url, json.dumps(data)) assert response.status_code == 200 response_user_ids = set([u["user"] for u in response.data]) user_ids = {other.id, joseph.id} assert (user_ids.issubset(response_user_ids))
def test_api_move_tasks_to_another_sprint_close_previous(client): project = f.create_project() f.MembershipFactory.create(project=project, user=project.owner, is_admin=True) milestone1 = f.MilestoneFactory.create(project=project) milestone2 = f.MilestoneFactory.create(project=project) closed_status = f.TaskStatusFactory.create(project=project, is_closed=True) task1 = f.create_task(project=project, milestone=milestone1, taskboard_order=1, status=closed_status, user_story=None) task2 = f.create_task(project=project, milestone=milestone1, taskboard_order=2, user_story=None) assert project.milestones.get(id=milestone1.id).tasks.count() == 2 assert not milestone1.closed url = reverse("milestones-move-tasks-to-sprint", kwargs={"pk": milestone1.pk}) data = { "project_id": project.id, "milestone_id": milestone2.id, "bulk_tasks": [{ "task_id": task2.id, "order": 2 }] } client.login(project.owner) response = client.json.post(url, json.dumps(data)) assert response.status_code == 204, response.data assert project.milestones.get(id=milestone1.id).tasks.count() == 1 assert project.milestones.get(id=milestone2.id).tasks.count() == 1 assert project.milestones.get(id=milestone1.id).closed
def test_api_create_bulk_members_with_enough_memberships_public_project_slots_multiple_projects( client): user = f.UserFactory.create(max_memberships_public_projects=6) project = f.ProjectFactory(owner=user, is_private=False) role = f.RoleFactory(project=project, name="Test") f.MembershipFactory(project=project, user=user, is_admin=True) other_project = f.ProjectFactory(owner=user) f.MembershipFactory.create(project=other_project) f.MembershipFactory.create(project=other_project) f.MembershipFactory.create(project=other_project) f.MembershipFactory.create(project=other_project) url = reverse("memberships-bulk-create") data = { "project_id": project.id, "bulk_memberships": [ { "role_id": role.pk, "username": "******" }, { "role_id": role.pk, "username": "******" }, { "role_id": role.pk, "username": "******" }, { "role_id": role.pk, "username": "******" }, ] } client.login(user) response = client.json.post(url, json.dumps(data)) assert response.status_code == 200
def test_history_with_only_comment_shouldnot_be_hidden(client): project = f.create_project() us = f.create_userstory(project=project, status__project=project) f.MembershipFactory.create(project=project, user=project.owner, is_admin=True) qs_all = HistoryEntry.objects.all() qs_hidden = qs_all.filter(is_hidden=True) assert qs_all.count() == 0 url = reverse("userstories-detail", args=[us.pk]) data = json.dumps({"comment": "test comment", "version": us.version}) client.login(project.owner) response = client.patch(url, data, content_type="application/json") assert response.status_code == 200, str(response.content) assert qs_all.count() == 1 assert qs_hidden.count() == 0
def test_update_epic_order(client): user = f.UserFactory.create() project = f.ProjectFactory.create(owner=user) epic_1 = f.EpicFactory.create(project=project, epics_order=1, status=project.default_us_status) epic_2 = f.EpicFactory.create(project=project, epics_order=2, status=project.default_us_status) epic_3 = f.EpicFactory.create(project=project, epics_order=3, status=project.default_us_status) f.MembershipFactory.create(project=project, user=user, is_admin=True) url = reverse('epics-detail', kwargs={"pk": epic_1.pk}) data = { "epics_order": 2, "version": epic_1.version } client.login(user) response = client.json.patch(url, json.dumps(data)) assert json.loads(response.get("tuesmon-info-order-updated")) == { str(epic_1.id): 2, str(epic_2.id): 3, str(epic_3.id): 4 }
def test_create_comment_on_public_project(client): user = f.UserFactory.create() project = f.ProjectFactory.create(is_private=False) project.is_contact_activated = True m1 = f.MembershipFactory(user=project.owner, project=project) m2 = f.MembershipFactory(project=project, is_admin=True) url = reverse("contact-list") contact_data = json.dumps({ "project": project.id, "comment": "Testing comment" }) client.login(user) assert len(mail.outbox) == 0 response = client.post(url, contact_data, content_type="application/json") assert response.status_code == 201 assert len(mail.outbox) == 1 assert set(mail.outbox[0].to[0].split(", ")) == set( [project.owner.email, m2.user.email])
def handle(self, *args, **options): dump_file_path = options["dump_file"] owner_email = options["owner_email"] overwrite = options["overwrite"] data = json.loads(open(dump_file_path, 'r').read()) try: if overwrite: receivers_back = signals.post_delete.receivers signals.post_delete.receivers = [] try: proj = Project.objects.get( slug=data.get("slug", "not a slug")) proj.tasks.all().delete() proj.user_stories.all().delete() proj.issues.all().delete() proj.memberships.all().delete() proj.roles.all().delete() proj.delete() except Project.DoesNotExist: pass signals.post_delete.receivers = receivers_back else: slug = data.get('slug', None) if slug is not None and Project.objects.filter( slug=slug).exists(): del data['slug'] user = User.objects.get(email=owner_email) services.store_project_from_dict(data, user) except err.TuesmonImportError as e: if e.project: e.project.delete_related_content() e.project.delete() print("ERROR:", end=" ") print(e.message) print(json.dumps(e.errors, indent=4))
def test_api_create_bulk_members_for_admin_without_enough_memberships_private_project_slots_one_project( client): owner = f.UserFactory.create(max_memberships_private_projects=3) user = f.UserFactory.create() project = f.ProjectFactory(owner=owner, is_private=True) role = f.RoleFactory(project=project, name="Test") f.MembershipFactory(project=project, user=user, is_admin=True) url = reverse("memberships-bulk-create") data = { "project_id": project.id, "bulk_memberships": [ { "role_id": role.pk, "username": "******" }, { "role_id": role.pk, "username": "******" }, { "role_id": role.pk, "username": "******" }, { "role_id": role.pk, "username": "******" }, ] } client.login(user) response = client.json.post(url, json.dumps(data)) assert response.status_code == 400 assert "reached your current limit of memberships for private" in response.data[ "_error_message"]
def test_api_update_milestone_in_bulk(client): user = f.UserFactory.create() project = f.ProjectFactory.create(owner=user, default_task_status=None) f.MembershipFactory.create(project=project, user=user, is_admin=True) milestone1 = f.MilestoneFactory(project=project) milestone2 = f.MilestoneFactory(project=project) task1 = f.create_task(project=project, milestone=milestone1) task2 = f.create_task(project=project, milestone=milestone1) task3 = f.create_task(project=project, milestone=milestone1) url = reverse("tasks-bulk-update-milestone") data = { "project_id": project.id, "milestone_id": milestone2.id, "bulk_tasks": [{ "task_id": task1.id, "order": 1 }, { "task_id": task2.id, "order": 2 }, { "task_id": task3.id, "order": 3 }] } client.login(project.owner) response = client.json.post(url, json.dumps(data)) assert response.status_code == 200, response.data assert response.data[task1.id] == milestone2.id assert response.data[task2.id] == milestone2.id assert response.data[task3.id] == milestone2.id
def test_api_move_userstories_to_another_sprint_close_previous(client): project = f.create_project() f.MembershipFactory.create(project=project, user=project.owner, is_admin=True) milestone1 = f.MilestoneFactory.create(project=project) milestone2 = f.MilestoneFactory.create(project=project) closed_status = f.UserStoryStatusFactory.create(is_closed=True) us1 = f.create_userstory(project=project, milestone=milestone1, sprint_order=1, status=closed_status) us2 = f.create_userstory(project=project, milestone=milestone1, sprint_order=2) assert milestone1.user_stories.count() == 2 assert not milestone1.closed url = reverse("milestones-move-userstories-to-sprint", kwargs={"pk": milestone1.pk}) data = { "project_id": project.id, "milestone_id": milestone2.id, "bulk_stories": [{ "us_id": us2.id, "order": 2 }] } client.login(project.owner) response = client.json.post(url, json.dumps(data)) assert response.status_code == 204, response.data assert project.milestones.get(id=milestone1.id).user_stories.count() == 1 assert project.milestones.get(id=milestone2.id).user_stories.count() == 1 assert project.milestones.get(id=milestone1.id).closed
def test_user_action_change_password_from_recovery(client, data): url = reverse('users-change-password-from-recovery') new_user = f.UserFactory(token="test-token") def reset_token(): new_user.token = "test-token" new_user.save() users = [ None, data.registered_user, data.other_user, data.superuser, ] patch_data = json.dumps({ "password": "******", "token": "test-token" }) results = helper_test_http_method(client, 'post', url, patch_data, users, reset_token) assert results == [204, 204, 204, 204]
def test_api_create_in_bulk_with_status_milestone(client): user = f.UserFactory.create() project = f.ProjectFactory.create(owner=user, default_task_status=None) f.MembershipFactory.create(project=project, user=user, is_admin=True) project.default_issue_status = f.IssueStatusFactory.create(project=project) project.save() milestone = f.MilestoneFactory(project=project) url = reverse("issues-bulk-create") data = { "bulk_issues": "Issue #1\nIssue #2", "project_id": project.id, "milestone_id": milestone.id, "status_id": project.default_issue_status.id } client.login(user) response = client.json.post(url, json.dumps(data)) assert response.status_code == 200 assert response.data[0]["status"] == project.default_issue_status.id assert response.data[0]["milestone"] == milestone.id
def test_api_create_membership_with_enough_memberships_public_project_slots_multiple_projects( client): user = f.UserFactory.create(max_memberships_public_projects=5) project = f.ProjectFactory(owner=user, is_private=False) role = f.RoleFactory(project=project, name="Test") f.MembershipFactory(project=project, user=user, is_admin=True) other_project = f.ProjectFactory(owner=user) f.MembershipFactory.create(project=other_project) f.MembershipFactory.create(project=other_project) f.MembershipFactory.create(project=other_project) f.MembershipFactory.create(project=other_project) client.login(user) url = reverse("memberships-list") data = { "role": role.pk, "project": project.pk, "username": "******" } response = client.json.post(url, json.dumps(data)) assert response.status_code == 201
def test_user_action_change_password(client, data): url = reverse('users-change-password') data.registered_user.set_password("test-current-password") data.registered_user.save() data.other_user.set_password("test-current-password") data.other_user.save() data.superuser.set_password("test-current-password") data.superuser.save() users = [ None, data.registered_user, data.other_user, data.superuser, ] post_data = json.dumps({ "current_password": "******", "password": "******" }) results = helper_test_http_method(client, 'post', url, post_data, users) assert results == [401, 204, 204, 204]
def test_user_action_change_email(client, data): url = reverse('users-change-email') def after_each_request(): data.registered_user.email_token = "test-token" data.registered_user.new_email = "*****@*****.**" data.registered_user.save() users = [ None, data.registered_user, data.other_user, ] patch_data = json.dumps({"email_token": "test-token"}) after_each_request() results = helper_test_http_method(client, 'post', url, patch_data, users, after_each_request=after_each_request) assert results == [204, 204, 204]