|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ce/api/invitations.html |
| 3 | +""" |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | +import pytest |
| 8 | +import responses |
| 9 | + |
| 10 | +from gitlab.exceptions import GitlabInvitationError |
| 11 | + |
| 12 | +create_content = {"email": "email@example.com", "access_level": 30} |
| 13 | +success_content = {"status": "success"} |
| 14 | +error_content = { |
| 15 | + "status": "error", |
| 16 | + "message": { |
| 17 | + "test@example.com": "Invite email has already been taken", |
| 18 | + "test2@example.com": "User already exists in source", |
| 19 | + "test_username": "Access level is not included in the list", |
| 20 | + }, |
| 21 | +} |
| 22 | +invitations_content = [ |
| 23 | + { |
| 24 | + "id": 1, |
| 25 | + "invite_email": "member@example.org", |
| 26 | + "created_at": "2020-10-22T14:13:35Z", |
| 27 | + "access_level": 30, |
| 28 | + "expires_at": "2020-11-22T14:13:35Z", |
| 29 | + "user_name": "Raymond Smith", |
| 30 | + "created_by_name": "Administrator", |
| 31 | + }, |
| 32 | +] |
| 33 | +invitation_content = { |
| 34 | + "expires_at": "2012-10-22T14:13:35Z", |
| 35 | + "access_level": 40, |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def resp_invitations_list(): |
| 41 | + with responses.RequestsMock() as rsps: |
| 42 | + rsps.add( |
| 43 | + method=responses.GET, |
| 44 | + url=re.compile(r"http://localhost/api/v4/(groups|projects)/1/invitations"), |
| 45 | + json=invitations_content, |
| 46 | + content_type="application/json", |
| 47 | + status=200, |
| 48 | + ) |
| 49 | + yield rsps |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def resp_invitation_create(): |
| 54 | + with responses.RequestsMock() as rsps: |
| 55 | + rsps.add( |
| 56 | + method=responses.POST, |
| 57 | + url=re.compile(r"http://localhost/api/v4/(groups|projects)/1/invitations"), |
| 58 | + json=success_content, |
| 59 | + content_type="application/json", |
| 60 | + status=200, |
| 61 | + ) |
| 62 | + yield rsps |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture |
| 66 | +def resp_invitation_create_error(): |
| 67 | + with responses.RequestsMock() as rsps: |
| 68 | + rsps.add( |
| 69 | + method=responses.POST, |
| 70 | + url=re.compile(r"http://localhost/api/v4/(groups|projects)/1/invitations"), |
| 71 | + json=error_content, |
| 72 | + content_type="application/json", |
| 73 | + status=200, |
| 74 | + ) |
| 75 | + yield rsps |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture |
| 79 | +def resp_invitation_update(): |
| 80 | + with responses.RequestsMock() as rsps: |
| 81 | + pattern = re.compile( |
| 82 | + r"http://localhost/api/v4/(groups|projects)/1/invitations/email%40example.com" |
| 83 | + ) |
| 84 | + rsps.add( |
| 85 | + method=responses.PUT, |
| 86 | + url=pattern, |
| 87 | + json=invitation_content, |
| 88 | + content_type="application/json", |
| 89 | + status=200, |
| 90 | + ) |
| 91 | + yield rsps |
| 92 | + |
| 93 | + |
| 94 | +@pytest.fixture |
| 95 | +def resp_invitation_delete(): |
| 96 | + with responses.RequestsMock() as rsps: |
| 97 | + pattern = re.compile( |
| 98 | + r"http://localhost/api/v4/(groups|projects)/1/invitations/email%40example.com" |
| 99 | + ) |
| 100 | + rsps.add( |
| 101 | + method=responses.DELETE, |
| 102 | + url=pattern, |
| 103 | + status=204, |
| 104 | + ) |
| 105 | + yield rsps |
| 106 | + |
| 107 | + |
| 108 | +def test_list_group_invitations(group, resp_invitations_list): |
| 109 | + invitations = group.invitations.list() |
| 110 | + assert invitations[0].invite_email == "member@example.org" |
| 111 | + |
| 112 | + |
| 113 | +def test_create_group_invitation(group, resp_invitation_create): |
| 114 | + invitation = group.invitations.create(create_content) |
| 115 | + assert invitation.status == "success" |
| 116 | + |
| 117 | + |
| 118 | +def test_update_group_invitation(group, resp_invitation_update): |
| 119 | + invitation = group.invitations.get("email@example.com", lazy=True) |
| 120 | + invitation.access_level = 30 |
| 121 | + invitation.save() |
| 122 | + |
| 123 | + |
| 124 | +def test_delete_group_invitation(group, resp_invitation_delete): |
| 125 | + invitation = group.invitations.get("email@example.com", lazy=True) |
| 126 | + invitation.delete() |
| 127 | + group.invitations.delete("email@example.com") |
| 128 | + |
| 129 | + |
| 130 | +def test_list_project_invitations(project, resp_invitations_list): |
| 131 | + invitations = project.invitations.list() |
| 132 | + assert invitations[0].invite_email == "member@example.org" |
| 133 | + |
| 134 | + |
| 135 | +def test_create_project_invitation(project, resp_invitation_create): |
| 136 | + invitation = project.invitations.create(create_content) |
| 137 | + assert invitation.status == "success" |
| 138 | + |
| 139 | + |
| 140 | +def test_update_project_invitation(project, resp_invitation_update): |
| 141 | + invitation = project.invitations.get("email@example.com", lazy=True) |
| 142 | + invitation.access_level = 30 |
| 143 | + invitation.save() |
| 144 | + |
| 145 | + |
| 146 | +def test_delete_project_invitation(project, resp_invitation_delete): |
| 147 | + invitation = project.invitations.get("email@example.com", lazy=True) |
| 148 | + invitation.delete() |
| 149 | + project.invitations.delete("email@example.com") |
| 150 | + |
| 151 | + |
| 152 | +def test_create_group_invitation_raises(group, resp_invitation_create_error): |
| 153 | + with pytest.raises(GitlabInvitationError, match="User already exists"): |
| 154 | + group.invitations.create(create_content) |
| 155 | + |
| 156 | + |
| 157 | +def test_create_project_invitation_raises(project, resp_invitation_create_error): |
| 158 | + with pytest.raises(GitlabInvitationError, match="User already exists"): |
| 159 | + project.invitations.create(create_content) |
0 commit comments