import json from enum import Enum from unittest.mock import patch from tests.test_utils import FixtureLoader from models import models from tests import BaseTestCase, mock_for_user from tests.artists.fixtures.artist_team_per_project_fixture import fixture from config import LABEL_IMAGE_SERVICE_URL class TestUserProjectRoles(Enum): ADMIN = 0 EDITOR = 1 LABELMATE = 2 VIEWER = 3 user1 = mock_for_user(1, "dev-marketing-user-1@example.com", "sme-dna|5cf0091d6c91d310fe6fcdc6", is_admin=True) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) class TestArtistTeam(BaseTestCase): def setUp(self): super().setUp() FixtureLoader(models=models).import_as_sql(fixture) def test_get_artist_teams_for_artist(self): url = "/artists/GRAS_10/artist-teams" response = self.execute_get(url, headers=self.headers) data = response.json self.assertEqual(200, response.status_code) self.soft_assert(self.assertEqual, data[0]["artistTeamId"], 1) self.soft_assert( self.assertEqual, data[0]["label"], {"id": 1, "name": "Columbia Records", "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1)} ) self.soft_assert(self.assertEqual, len(data[0]["users"]), 3) self.soft_assert( self.assertEqual, data[0]["users"][0], { "email": "dev-marketing-user-1@example.com", "id": 1, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1) }, { "id": 2, "name": "Epic Records", "externalId": "223456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=2) }, { "id": 3, "name": "RCA Records", "externalId": "323456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=3) }, { "id": 4, "name": "CMG", "externalId": "423456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=4) }, ], "name": "user-1", "phone": "+1234234", "roles": [{"categoryIds": [], "id": 1, "name": "Editor"}], }, ) self.soft_assert( self.assertEqual, data[0]["users"][1], { "email": "dev-marketing-user-3@example.com", "id": 3, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1) }, { "id": 2, "name": "Epic Records", "externalId": "223456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=2) }, ], "name": "user-3", "phone": "", "roles": [{"categoryIds": [], "id": 1, "name": "Editor"}], }, ) self.soft_assert( self.assertEqual, data[0]["users"][2], { "email": "dev-marketing-user-4@example.com", "id": 4, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1) } ], "name": "user-4", "phone": "", "roles": [{"categoryIds": [], "id": 3, "name": "Viewer"}], }, ) self.assert_all() @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) class TestManageArtistTeam(BaseTestCase): url = "/artists/GRAS_10/artist-teams" def setUp(self): super().setUp() FixtureLoader(models=models).import_as_sql(fixture) def add_user_to_artist_team(self, user_id: int, role_id: int, label_id: int, categories_id=[]): data = {"labelId": label_id, "added": [{"userId": user_id, "roleId": role_id, "categoryIds": categories_id}]} return self.execute_post(self.url, data=json.dumps(data), headers=self.headers) def test_create_new_artist_team(self): url = "/artists/GRAS_20/artist-teams" data = {"labelId": 3, "added": [{"userId": 2, "roleId": 1, "categoryIds": [1, 2, 3]}]} response = self.execute_post(url, data=json.dumps(data), headers=self.headers) self.assertEqual(200, response.status_code) data = response.json self.soft_assert(self.assertEqual, data["artistTeamId"], 4) self.soft_assert( self.assertEqual, data["label"], {"id": 3, "name": "RCA Records", "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=3)} ) self.soft_assert(self.assertEqual, len(data["users"]), 1) self.soft_assert( self.assertEqual, data["users"], [ { "email": "dev-marketing-user-2@example.com", "id": 2, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), }, { "id": 3, "name": "RCA Records", "externalId": "323456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=3), }, ], "name": "user-2", "phone": "", "roles": [{"categoryIds": [1, 2, 3], "id": 1, "name": "Editor"}], } ], ) self.assert_all() def test_create_new_artist_team_with_editor_without_category_should_not_create_anything(self): url = "/artists/GRAS_20/artist-teams" data = {"labelId": 3, "added": [ {"userId": 2, "roleId": TestUserProjectRoles.EDITOR.value, "categoryIds": [1, 2, 3]}, {"userId": 5, "roleId": TestUserProjectRoles.EDITOR.value, "categoryIds": []} ]} response = self.execute_get(url, headers=self.headers) artist_teams_before_creation = response.json response = self.execute_post(url, data=json.dumps(data), headers=self.headers) self.assertEqual(400, response.status_code) response = self.execute_get(url, headers=self.headers) artist_teams_after_creation = response.json self.soft_assert(self.assertEqual, artist_teams_before_creation, artist_teams_after_creation) self.assert_all() def test_add_editor_to_artist_team(self): data = {"labelId": 1, "added": [{"userId": 2, "roleId": TestUserProjectRoles.EDITOR.value, "categoryIds": [1, 2]}]} response = self.execute_post(self.url, data=json.dumps(data), headers=self.headers) self.assertEqual(200, response.status_code) data = response.json self.soft_assert(self.assertEqual, data["artistTeamId"], 1) self.soft_assert( self.assertEqual, data["label"], {"id": 1, "name": "Columbia Records", "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1)} ) self.soft_assert(self.assertEqual, len(data["users"]), 4) self.soft_assert( self.assertEqual, data["users"][0], { "email": "dev-marketing-user-1@example.com", "id": 1, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), }, { "id": 2, "name": "Epic Records", "externalId": "223456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=2), }, { "id": 3, "name": "RCA Records", "externalId": "323456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=3), }, { "id": 4, "name": "CMG", "externalId": "423456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=4), }, ], "name": "user-1", "phone": "+1234234", "roles": [{"categoryIds": [], "id": 1, "name": "Editor"}], }, ) self.soft_assert( self.assertEqual, data["users"][1], { "email": "dev-marketing-user-2@example.com", "id": 2, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), }, { "id": 3, "name": "RCA Records", "externalId": "323456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=3), }, ], "name": "user-2", "phone": "", "roles": [{"categoryIds": [1, 2], "id": 1, "name": "Editor"}], }, ) self.soft_assert( self.assertEqual, data["users"][2], { "email": "dev-marketing-user-3@example.com", "id": 3, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), }, { "id": 2, "name": "Epic Records", "externalId": "223456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=2) }, ], "name": "user-3", "phone": "", "roles": [{"categoryIds": [], "id": 1, "name": "Editor"}], }, ) self.soft_assert( self.assertEqual, data["users"][3], { "email": "dev-marketing-user-4@example.com", "id": 4, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), } ], "name": "user-4", "phone": "", "roles": [{"categoryIds": [], "id": 3, "name": "Viewer"}], }, ) self.assert_all() def test_update_editor_to_viewer_in_artist_team(self): data = {"labelId": 1, "updated": [{"userId": 3, "roleId": TestUserProjectRoles.VIEWER.value, "categoryIds": []}]} response = self.execute_post(self.url, data=json.dumps(data), headers=self.headers) self.assertEqual(200, response.status_code) data = response.json self.soft_assert(self.assertEqual, data["artistTeamId"], 1) self.soft_assert( self.assertEqual, data["label"], { "id": 1, "name": "Columbia Records", "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), } ) self.soft_assert(self.assertEqual, len(data["users"]), 3) self.soft_assert( self.assertEqual, data["users"][0], { "email": "dev-marketing-user-1@example.com", "id": 1, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), }, { "id": 2, "name": "Epic Records", "externalId": "223456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=2), }, { "id": 3, "name": "RCA Records", "externalId": "323456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=3), }, { "id": 4, "name": "CMG", "externalId": "423456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=4), }, ], "name": "user-1", "phone": "+1234234", "roles": [{"categoryIds": [], "id": 1, "name": "Editor"}], }, ) self.soft_assert( self.assertEqual, data["users"][1], { "email": "dev-marketing-user-3@example.com", "id": 3, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), }, { "id": 2, "name": "Epic Records", "externalId": "223456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=2) }, ], "name": "user-3", "phone": "", "roles": [{"categoryIds": [], "id": 3, "name": "Viewer"}], }, ) self.soft_assert( self.assertEqual, data["users"][2], { "email": "dev-marketing-user-4@example.com", "id": 4, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), } ], "name": "user-4", "phone": "", "roles": [{"categoryIds": [], "id": 3, "name": "Viewer"}], }, ) self.assert_all() def test_delete_user_from_artist_team(self): data = {"labelId": 1, "deleted": [{"userId": 3, "roleId": TestUserProjectRoles.EDITOR.value, "categoryIds": []}]} response = self.execute_post(self.url, data=json.dumps(data), headers=self.headers) self.assertEqual(200, response.status_code) data = response.json self.soft_assert(self.assertEqual, data["artistTeamId"], 1) self.soft_assert( self.assertEqual, data["label"], {"id": 1, "name": "Columbia Records", "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1)}) self.soft_assert(self.assertEqual, len(data["users"]), 2) self.soft_assert( self.assertEqual, data["users"][0], { "email": "dev-marketing-user-1@example.com", "id": 1, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), }, { "id": 2, "name": "Epic Records", "externalId": "223456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=2), }, { "id": 3, "name": "RCA Records", "externalId": "323456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=3), }, { "id": 4, "name": "CMG", "externalId": "423456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=4), }, ], "name": "user-1", "phone": "+1234234", "roles": [{"categoryIds": [], "id": 1, "name": "Editor"}], }, ) self.soft_assert( self.assertEqual, data["users"][1], { "email": "dev-marketing-user-4@example.com", "id": 4, "labels": [ { "id": 1, "name": "Columbia Records", "externalId": "123456", "abbreviation": None, "country": None, "imageUrl": LABEL_IMAGE_SERVICE_URL.format(id=1), } ], "name": "user-4", "phone": "", "roles": [{"categoryIds": [], "id": 3, "name": "Viewer"}], }, ) self.assert_all() def test_add_user_to_artist_team_user_should_not_be_deleted_from_confidential_project_team(self): data = {"labelId": 2, "added": [{"userId": 3, "roleId": TestUserProjectRoles.EDITOR.value, "categoryIds": [1]}]} add_artist_team_user_response = self.execute_post(self.url, data=json.dumps(data), headers=self.headers) self.assertEqual(200, add_artist_team_user_response.status_code) project_team_response = self.client.get("/projects/506/team", headers=self.headers) project_team = project_team_response.json self.soft_assert(self.assertEqual, project_team[0]["id"], 3) self.soft_assert(self.assertEqual, project_team[0]["email"], "dev-marketing-user-3@example.com") self.assert_all() def test_add_user_to_not_primary_locked_artists_team_should_not_be_deleted_from_project_team(self): data = {"labelId": 1, "added": [{"userId": 2, "roleId": TestUserProjectRoles.EDITOR.value, "categoryIds": [1]}]} add_artist_team_user_response = self.execute_post(self.url, data=json.dumps(data), headers=self.headers) self.assertEqual(200, add_artist_team_user_response.status_code) project_team_response = self.client.get("/projects/503/team", headers=self.headers) project_team = project_team_response.json self.soft_assert(self.assertEqual, project_team[0]["id"], 2) self.soft_assert(self.assertEqual, project_team[0]["email"], "dev-marketing-user-2@example.com") self.assert_all()