from unittest.mock import patch import json from sqlalchemy import and_ from db import db from tests.test_utils import FixtureLoader from models import models, ArtistTeamUser, ArtistTeam, Artist, Project, MediaPlan from tests import BaseTestCase, mock_for_user from tests.label_permissions.fixtures.label_permissions_artist_teams_fixture import fixture user1 = mock_for_user(1, "dev-marketing-user-1@example.com", "sme-dna|5cf0091d6c91d310fe6fcdc6", is_admin=True) user2 = mock_for_user(2, "dev-marketing-user-2@example.com", "sme-dna|5cf0091d6c91d310fe6fcdc7", is_admin=False) class TestLabelArtists(BaseTestCase): url = "/labels/{}/permissions/users/{}/artist-teams" def get_user_projects_for_user_id(self, user_id: int): return ( db.session.execute(f""" SELECT project_id FROM user_projects WHERE user_id = {user_id} """).fetchall() ) def setUp(self): super().setUp() FixtureLoader(models=models).import_as_sql(fixture) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_create_artist_team_for_user(self): data = { "artistId": ["GRAS_10"] } response = self.execute_post(self.url.format(1, 2), headers=self.headers, data=json.dumps(data)) self.assertEqual(204, response.status_code) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_delete_artist_team_for_user(self): data = { "artistId": ["GRAS_10"] } response = self.execute_delete( self.url.format(1, 1) + "?" + "".join(["artistId="+artist for artist in data.get("artistId")]), headers=self.headers ) self.assertEqual(204, response.status_code) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user2.jwt_decode) def test_not_admin_403(self): data = { "artistId": ["GRAS_10"] } response = self.execute_post(self.url.format(1, 2), headers=self.headers, data=json.dumps(data)) self.assertEqual(403, response.status_code) response = self.execute_delete( self.url.format(1, 2) + "?" + "".join(["artistId="+artist for artist in data.get("artistId")]), headers=self.headers ) self.assertEqual(403, response.status_code) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_artist_exist_not_exist_or_not_from_this_label(self): data = { "artistId": ["GRAS_42"] } response = self.execute_post(self.url.format(1, 2), headers=self.headers, data=json.dumps(data)) self.assertEqual(422, response.status_code) response = self.execute_delete( self.url.format(1, 2) + "?" + "".join(["artistId="+artist for artist in data.get("artistId")]), headers=self.headers ) self.assertEqual(422, response.status_code) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_add_user_to_artist_team_created_and_project_claimed(self): artist_id = ["GRAS_20", "GRAS_30"] data = { "artistId": artist_id } user_id = 2 response = self.execute_post(self.url.format(1, user_id), headers=self.headers, data=json.dumps(data)) self.assertEqual(204, response.status_code) artist_team_users = ( db.session.query(ArtistTeamUser.id) .select_from(Artist) .join(ArtistTeam, and_(ArtistTeam.artist_id == Artist.id, ArtistTeam.label_id == 1)) .join(ArtistTeamUser, and_( ArtistTeamUser.user_id == user_id, ArtistTeamUser.artist_team_id == ArtistTeam.id )) .filter(Artist.external_id.in_(artist_id)) .all() ) self.soft_assert_equal(len(artist_team_users), 2) project_504 = ( db.session.query(Project) .filter(Project.id == 504) .first() ) self.soft_assert_equal(project_504.is_claimed, True) project_511 = ( db.session.query(Project) .filter(Project.id == 511) .first() ) self.soft_assert_equal(project_511.is_claimed, True) self.soft_assert(self.assertIsNotNone, project_511.end_date) media_plan = ( db.session.query(MediaPlan) .filter(MediaPlan.project_id == 511) .one_or_none() ) self.soft_assert(self.assertIsNotNone, media_plan) user_projects = self.get_user_projects_for_user_id(user_id) self.soft_assert_equal( set([data[0] for data in user_projects]), {"PRS_012345565", "PRS_012345563", "PRS_012345561", "PRS_012345562"} ) response = self.execute_delete( self.url.format(1, user_id) + "?artistId=GRAS_20&artistId=GRAS_30", headers=self.headers ) self.soft_assert_equal(204, response.status_code) user_projects = self.get_user_projects_for_user_id(user_id) self.soft_assert_equal( set([data[0] for data in user_projects]), {"PRS_012345565", } ) self.assert_all() @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_delete_user_from_artist_remove_artist_team_user_and_team_if_lasat_user(self): data = { "artistId": ["GRAS_10"] } artist_team_user_before_deletion = ( db.session.query(ArtistTeamUser) .select_from(ArtistTeam) .join(ArtistTeamUser, ArtistTeamUser.artist_team_id == ArtistTeam.id) .filter(ArtistTeamUser.user_id == 1, ArtistTeam.label_id == 1).count() ) response = self.execute_delete( self.url.format(1, 1) + "?" + "".join(["artistId=" + artist for artist in data.get("artistId")]), headers=self.headers ) self.assertEqual(204, response.status_code) artist_team_user_after_deletion = ( db.session.query(ArtistTeamUser) .select_from(ArtistTeam) .join(ArtistTeamUser, ArtistTeamUser.artist_team_id == ArtistTeam.id) .filter(ArtistTeamUser.user_id == 1, ArtistTeam.label_id == 1).count() ) self.assertEqual(artist_team_user_before_deletion - 1, artist_team_user_after_deletion) artist_team_user = ( db.session.query(ArtistTeam) .join(ArtistTeamUser, and_(ArtistTeamUser.artist_team_id == ArtistTeam.id, ArtistTeamUser.user_id == 1)) .filter(ArtistTeam.label_id == 1, ArtistTeam.artist_id == 10) .one_or_none() ) self.assertIsNone(artist_team_user) artist_team = db.session.query(ArtistTeam).filter(ArtistTeam.artist_id == 10, ArtistTeam.label_id == 1).one_or_none() self.assertIsNotNone(artist_team) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_delete_user_from_artist_remove_team_if_last_user_removed(self): data = { "artistId": ["GRAS_80"] } response = self.execute_delete( self.url.format(3, 1) + "?" + "".join(["artistId=" + artist for artist in data.get("artistId")]), headers=self.headers ) self.assertEqual(204, response.status_code) artist_team = db.session.query(ArtistTeam).filter(ArtistTeam.artist_id == 80, ArtistTeam.label_id == 3).one_or_none() self.assertIsNone(artist_team) project_512 = ( db.session.query( Project ) .filter(Project.id == 512) .one() ) self.assertEqual(project_512.is_claimed, False) @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_add_user_to_primary_locked_artists_teams_should_remove_projects_collaborator(self): response = self.execute_get("/projects/513/team", headers=self.headers) self.soft_assert_equal(200, response.status_code) data = response.json self.soft_assert_equal(len(data), 2) artist_id = ["GRAS_90", "GRAS_100"] data = { "artistId": artist_id } user_id = 4 response = self.execute_post(self.url.format(3, user_id), headers=self.headers, data=json.dumps(data)) self.assertEqual(204, response.status_code) artist_team_users = ( db.session.query(ArtistTeamUser.id) .select_from(Artist) .join(ArtistTeam, and_(ArtistTeam.artist_id == Artist.id, ArtistTeam.label_id == 3)) .join(ArtistTeamUser, and_( ArtistTeamUser.user_id == user_id, ArtistTeamUser.artist_team_id == ArtistTeam.id )) .filter(Artist.external_id.in_(artist_id)) .all() ) self.soft_assert_equal(len(artist_team_users), 2) response = self.execute_get("/projects/513/team", headers=self.headers) self.soft_assert_equal(200, response.status_code) data = response.json self.soft_assert_equal(len(data), 1) self.assert_all() @patch("auth.atlas.atlas_token_decoder.jwt.decode", user1.jwt_decode) def test_add_user_to_not_primary_locked_artists_teams_should_not_remove_projects_collaborator(self): response = self.execute_get("/projects/514/team", headers=self.headers) self.soft_assert_equal(200, response.status_code) data = response.json self.soft_assert_equal(len(data), 2) artist_id = ["GRAS_100"] data = { "artistId": artist_id } user_id = 4 response = self.execute_post(self.url.format(3, user_id), headers=self.headers, data=json.dumps(data)) self.assertEqual(204, response.status_code) artist_team_users = ( db.session.query(ArtistTeamUser.id) .select_from(Artist) .join(ArtistTeam, and_(ArtistTeam.artist_id == Artist.id, ArtistTeam.label_id == 3)) .join(ArtistTeamUser, and_( ArtistTeamUser.user_id == user_id, ArtistTeamUser.artist_team_id == ArtistTeam.id )) .filter(Artist.external_id.in_(artist_id)) .all() ) self.soft_assert_equal(len(artist_team_users), 1) response = self.execute_get("/projects/513/team", headers=self.headers) self.soft_assert_equal(200, response.status_code) data = response.json self.soft_assert_equal(len(data), 2) self.assert_all()