|
| 1 | +import pytest |
| 2 | +from faker import Faker |
| 3 | +from labelbox.schema.media_type import MediaType |
| 4 | +from labelbox import ProjectRole |
| 5 | +import time |
| 6 | + |
| 7 | +faker = Faker() |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def dummy_email(): |
| 12 | + """Generate a random dummy email for testing""" |
| 13 | + return f"none+{faker.uuid4()}@labelbox.com" |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="module") |
| 17 | +def test_project(client): |
| 18 | + """Create a temporary project for testing""" |
| 19 | + project = client.create_project( |
| 20 | + name=f"test-project-{faker.uuid4()}", media_type=MediaType.Image |
| 21 | + ) |
| 22 | + yield project |
| 23 | + |
| 24 | + # Occurs after the test is finished based on scope |
| 25 | + project.delete() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def org_invite(client, dummy_email): |
| 30 | + """Create an organization-level invite""" |
| 31 | + role = client.get_roles()["LABELER"] |
| 32 | + organization = client.get_organization() |
| 33 | + invite = organization.invite_user(dummy_email, role) |
| 34 | + |
| 35 | + yield invite |
| 36 | + |
| 37 | + if invite.uid: |
| 38 | + invite.cancel() |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def project_invite(client, test_project, dummy_email): |
| 43 | + """Create a project-level invite""" |
| 44 | + roles = client.get_roles() |
| 45 | + project_role = ProjectRole(project=test_project, role=roles["LABELER"]) |
| 46 | + organization = client.get_organization() |
| 47 | + |
| 48 | + invite = organization.invite_user( |
| 49 | + dummy_email, roles["NONE"], project_roles=[project_role] |
| 50 | + ) |
| 51 | + |
| 52 | + yield invite |
| 53 | + |
| 54 | + # Cleanup: Use invite.cancel() instead of organization.cancel_invite() |
| 55 | + if invite.uid: |
| 56 | + invite.cancel() |
| 57 | + |
| 58 | + |
| 59 | +def test_get_organization_invites(client, org_invite): |
| 60 | + """Test retrieving all organization invites""" |
| 61 | + # Add a small delay to ensure invite is created |
| 62 | + time.sleep(1) |
| 63 | + |
| 64 | + organization = client.get_organization() |
| 65 | + invites = organization.get_invites() |
| 66 | + invite_list = [invite for invite in invites] |
| 67 | + assert len(invite_list) > 0 |
| 68 | + |
| 69 | + # Verify our test invite is in the list |
| 70 | + invite_emails = [invite.email for invite in invite_list] |
| 71 | + assert org_invite.email in invite_emails |
| 72 | + |
| 73 | + |
| 74 | +def test_get_project_invites(client, test_project, project_invite): |
| 75 | + """Test retrieving project-specific invites""" |
| 76 | + # Add a small delay to ensure invite is created |
| 77 | + time.sleep(1) |
| 78 | + |
| 79 | + organization = client.get_organization() |
| 80 | + project_invites = organization.get_project_invites(test_project.uid) |
| 81 | + invite_list = [invite for invite in project_invites] |
| 82 | + assert len(invite_list) > 0 |
| 83 | + |
| 84 | + # Verify our test invite is in the list |
| 85 | + invite_emails = [invite.email for invite in invite_list] |
| 86 | + assert project_invite.email in invite_emails |
| 87 | + |
| 88 | + # Verify project role assignment |
| 89 | + found_invite = next( |
| 90 | + invite for invite in invite_list if invite.email == project_invite.email |
| 91 | + ) |
| 92 | + assert len(found_invite.project_roles) == 1 |
| 93 | + assert found_invite.project_roles[0].project.uid == test_project.uid |
| 94 | + |
| 95 | + |
| 96 | +def test_cancel_invite(client, dummy_email): |
| 97 | + """Test canceling an invite""" |
| 98 | + # Create a new invite |
| 99 | + role = client.get_roles()["LABELER"] |
| 100 | + organization = client.get_organization() |
| 101 | + organization.invite_user(dummy_email, role) |
| 102 | + |
| 103 | + # Add a small delay to ensure invite is created |
| 104 | + time.sleep(1) |
| 105 | + |
| 106 | + # Find the actual invite by email |
| 107 | + invites = organization.get_invites() |
| 108 | + found_invite = next( |
| 109 | + (invite for invite in invites if invite.email == dummy_email), None |
| 110 | + ) |
| 111 | + assert found_invite is not None, f"Invite for {dummy_email} not found" |
| 112 | + |
| 113 | + # Cancel the invite using the found invite object |
| 114 | + result = found_invite.cancel() |
| 115 | + assert result is True |
| 116 | + |
| 117 | + # Verify the invite is no longer in the organization's invites |
| 118 | + invites = organization.get_invites() |
| 119 | + invite_emails = [i.email for i in invites] |
| 120 | + assert dummy_email not in invite_emails |
| 121 | + |
| 122 | + |
| 123 | +def test_cancel_project_invite(client, test_project, dummy_email): |
| 124 | + """Test canceling a project invite""" |
| 125 | + # Create a project invite |
| 126 | + roles = client.get_roles() |
| 127 | + project_role = ProjectRole(project=test_project, role=roles["LABELER"]) |
| 128 | + organization = client.get_organization() |
| 129 | + |
| 130 | + organization.invite_user( |
| 131 | + dummy_email, roles["NONE"], project_roles=[project_role] |
| 132 | + ) |
| 133 | + |
| 134 | + # Add a small delay to ensure invite is created |
| 135 | + time.sleep(1) |
| 136 | + |
| 137 | + # Find the actual invite by email |
| 138 | + invites = organization.get_invites() |
| 139 | + found_invite = next( |
| 140 | + (invite for invite in invites if invite.email == dummy_email), None |
| 141 | + ) |
| 142 | + assert found_invite is not None, f"Invite for {dummy_email} not found" |
| 143 | + |
| 144 | + # Cancel the invite using the found invite object |
| 145 | + result = found_invite.cancel() |
| 146 | + assert result is True |
| 147 | + |
| 148 | + # Verify the invite is no longer in the project's invites |
| 149 | + project_invites = organization.get_project_invites(test_project.uid) |
| 150 | + invite_emails = [i.email for i in project_invites] |
| 151 | + assert dummy_email not in invite_emails |
| 152 | + |
| 153 | + |
| 154 | +def test_project_invite_after_project_deletion(client, dummy_email): |
| 155 | + """Test that project invites are properly filtered when a project is deleted""" |
| 156 | + # Create two test projects |
| 157 | + project1 = client.create_project( |
| 158 | + name=f"test-project1-{faker.uuid4()}", media_type=MediaType.Image |
| 159 | + ) |
| 160 | + project2 = client.create_project( |
| 161 | + name=f"test-project2-{faker.uuid4()}", media_type=MediaType.Image |
| 162 | + ) |
| 163 | + |
| 164 | + # Create project roles |
| 165 | + roles = client.get_roles() |
| 166 | + project_role1 = ProjectRole(project=project1, role=roles["LABELER"]) |
| 167 | + project_role2 = ProjectRole(project=project2, role=roles["LABELER"]) |
| 168 | + |
| 169 | + # Invite user to both projects |
| 170 | + organization = client.get_organization() |
| 171 | + organization.invite_user( |
| 172 | + dummy_email, roles["NONE"], project_roles=[project_role1, project_role2] |
| 173 | + ) |
| 174 | + |
| 175 | + # Add a small delay to ensure invite is created |
| 176 | + time.sleep(1) |
| 177 | + |
| 178 | + # Delete one project |
| 179 | + project1.delete() |
| 180 | + |
| 181 | + # Find the invite and verify project roles |
| 182 | + invites = organization.get_invites() |
| 183 | + found_invite = next( |
| 184 | + (invite for invite in invites if invite.email == dummy_email), None |
| 185 | + ) |
| 186 | + assert found_invite is not None, f"Invite for {dummy_email} not found" |
| 187 | + |
| 188 | + # Verify only one project role remains |
| 189 | + assert ( |
| 190 | + len(found_invite.project_roles) == 1 |
| 191 | + ), "Expected only one project role" |
| 192 | + assert found_invite.project_roles[0].project.uid == project2.uid |
| 193 | + |
| 194 | + # Cleanup |
| 195 | + project2.delete() |
| 196 | + if found_invite.uid: |
| 197 | + found_invite.cancel() |
0 commit comments