def setUp(self): # init command output self.out = StringIO() # create place self.place = Place(name="Le Fresnoy", description="Le Fresnoy Studio National", address="22 rue du Fresnoy") self.place.save() # create Author self.user = User() self.user.first_name = "Andrew" self.user.last_name = "Warhola" self.user.username = "******" self.user.password = "******" self.user.save() # create artist self.artist = Artist(user=self.user, nickname="Andy Warhol") self.artist.save() # create arwork self.film = Film(title="title", production_date="2019-01-01") self.film.save() self.film.authors.add(self.artist) self.film.save() # create event self.event = Event(title='PanoramaX', starting_date="1970-01-01 00:00:00.0+00:00", type="FEST", place=self.place) self.event.save() self.event.films.add(self.film) self.event.save()
class ArtistEndPoint(TestCase): """ Tests Artist's endpoint """ def setUp(self): self.user = User() self.user.first_name = "Andrew" self.user.last_name = "Warhola" self.user.username = "******" self.user.save() self.artist = Artist(user=self.user, nickname="Andy Warhol") self.artist.save() self.response = None def tearDown(self): self.response = None def test_list(self): """ Test list of artist """ url = reverse('artist-list') self.response = self.client.get(url) self.assertEqual(self.response.status_code, 200)
def setUp(self): self.user = User() self.user.first_name = "Andrew" self.user.last_name = "Warhola" self.user.username = "******" self.user.save() self.artist = Artist(user=self.user, nickname="Andy Warhol") self.artist.save() self.response = None
class CommandsTestCase(TestCase): """ Tests Diffusion Commands """ def setUp(self): # init command output self.out = StringIO() # create place self.place = Place(name="Le Fresnoy", description="Le Fresnoy Studio National", address="22 rue du Fresnoy") self.place.save() # create Author self.user = User() self.user.first_name = "Andrew" self.user.last_name = "Warhola" self.user.username = "******" self.user.password = "******" self.user.save() # create artist self.artist = Artist(user=self.user, nickname="Andy Warhol") self.artist.save() # create arwork self.film = Film(title="title", production_date="2019-01-01") self.film.save() self.film.authors.add(self.artist) self.film.save() # create event self.event = Event(title='PanoramaX', starting_date="1970-01-01 00:00:00.0+00:00", type="FEST", place=self.place) self.event.save() self.event.films.add(self.film) self.event.save() def tearDown(self): pass def test_synchronize_diffusions(self): "simple TEST Command: synchronize_diffusion" call_command('synchronize_diffusions', stdout=self.out) diffusions = Diffusion.objects.all() self.assertEqual(diffusions.count(), 1) def test_place_creation(self): "simple TEST Command: create_place" call_command('create_city_place', 'Macondo', 'Colombia', stdout=self.out) place = Place.objects.all() self.assertEqual(place.count(), 2)
def setUp(self): self.user = User() self.user.first_name = "Andrew" self.user.last_name = "Warhola" self.user.username = "******" self.user.password = "******" self.user.save() # save generate token self.token = "" self.client_auth = APIClient() self.artist = Artist(user=self.user, nickname="Andy Warhol") self.artist.save()
def setUp(self): # create place self.place = Place(name="Le Fresnoy", description="Le Fresnoy Studio National", address="22 rue du Fresnoy") self.place.save() # create user self.user = User(first_name="Andrew", last_name="Warhola", username="******") self.user.save() # create Artist self.artist = Artist(user=self.user) self.artist.save()
class CommandsTestCase(TestCase): """ Tests Production Models """ def setUp(self): # create place self.place = Place(name="Le Fresnoy", description="Le Fresnoy Studio National", address="22 rue du Fresnoy") self.place.save() # create user self.user = User(first_name="Andrew", last_name="Warhola", username="******") self.user.save() # create Artist self.artist = Artist(user=self.user) self.artist.save() def tearDown(self): pass def test_event(self): "simple TEST create event" # create event self.event = Event(title='Panorama', starting_date="1970-01-01 00:00:00.0+00:00", type="EXIB", place=self.place) self.event.save() # get Events events = Event.objects.all() # test metaEvent created self.assertEqual(events.count(), 1) def test_production_film(self): "simple TEST create film" film = Film(title="Diptyque Marilyn", production_date=parse_date("1962-01-01")) film.save() # set author film.authors.set((self.artist, )) # get films films = Film.objects.all() # test film created self.assertEqual(films.count(), 1)
def create(self, request): """ This view create an application AND Artist for auth user """ user = self.request.user # first of all test current campaign if candidature_close() and not user.is_staff: errors = {'candidature': 'expired'} return Response(errors, status=status.HTTP_403_FORBIDDEN) campaign = StudentApplicationSetup.objects.filter( is_current_setup=True).first() # user muse be auth if user.is_authenticated: # is an current inscription current_year_application = StudentApplication.objects.filter( artist__user=user.id, campaign=campaign) if not current_year_application: # take the artist user_artist = Artist.objects.filter(user=user.id) if not user_artist: # if not, create it user_artist = Artist(user=user) user_artist.save() else: # take the first one user_artist = user_artist[0] # create application student_application = StudentApplication(artist=user_artist, campaign=campaign) student_application.save() return Response(status=status.HTTP_201_CREATED) else: # user can't create two application for this year errors = { 'candidature': 'you are not able to create another candidature this session' } return Response(errors, status=status.HTTP_409_CONFLICT) else: # FIXME: dead code: handled by APIView.permission_denied which raise HTTP_403 errors = {'candidature': 'forbidden'} return Response(errors, status=status.HTTP_403_FORBIDDEN)
def create(self, request): """ This view create an application AND Artist for auth user """ user = self.request.user # first of all test current campain if candidature_close() and not user.is_staff: errors = {'candidature': 'expired'} return Response(errors, status=status.HTTP_403_FORBIDDEN) campain = StudentApplicationSetup.objects.filter(is_current_setup=True).first() # user muse be auth if user.is_authenticated(): # is an current inscription current_year_application = StudentApplication.objects.filter( artist__user=user.id, campain=campain ) if not current_year_application: # take the artist user_artist = Artist.objects.filter(user=user.id) if not user_artist: # if not, create it user_artist = Artist(user=user) user_artist.save() else: # take the first one user_artist = user_artist[0] # create application student_application = StudentApplication(artist=user_artist, campain=campain) student_application.save() return Response(status=status.HTTP_201_CREATED) else: # user can't create two application for this year errors = {'candidature': 'you are not able to create another candidature this session'} return Response(errors, status=status.HTTP_409_CONFLICT) else: errors = {'candidature': 'forbidden'} return Response(errors, status=status.HTTP_403_FORBIDDEN)
class TestApplicationEndPoint(TestCase): """ Tests concernants le endpoint des Student Application """ def setUp(self): self.user = User() self.user.first_name = "Andrew" self.user.last_name = "Warhola" self.user.username = "******" self.user.password = "******" self.user.save() # save generate token self.token = "" self.client_auth = APIClient() self.artist = Artist(user=self.user, nickname="Andy Warhol") self.artist.save() def tearDown(self): pass def _get_list(self): url = reverse('studentapplication-list') return self.client.get(url) def _get_list_auth(self): url = reverse('studentapplication-list') return self.client_auth.get(url) def test_list(self): """ Test list of applications without authentification """ # set up a candidature application = StudentApplication(artist=self.artist) application.save() self.token = "" response = self._get_list() candidatures = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(candidatures) self.assertEqual(len(candidatures), 1) # info is NOT accessible when anonymous user self.assertRaises(KeyError, lambda: candidatures[0]['current_year_application_count']) def test_list_auth(self): """ Test list of applications with authentification """ # set up a candidature application = StudentApplication(artist=self.artist) application.save() self.client_auth.force_authenticate(user=self.user) response = self._get_list_auth() candidature = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) # info is accessible when user is auth assert candidature[0]['current_year_application_count'] is not None def test_create_student_application(self): """ Test creating an studentapplication """ self.client_auth.force_authenticate(user=self.user) studentapplication_url = reverse('studentapplication-list') response = self.client_auth.post(studentapplication_url) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(StudentApplication.objects.count(), 1) self.assertEqual(StudentApplication.objects.last().artist.user.first_name, self.user.first_name) def test_update_student_application(self): """ Test creating an studentapplication """ # set up a campain promotion = Promotion(starting_year=2000, ending_year=2001) promotion.save() campain = StudentApplicationSetup(candidature_date_start=timezone.now(), candidature_date_end=timezone.now() + datetime.timedelta(days=1), promotion=promotion, is_current_setup=True,) campain.save() # add a candidature application = StudentApplication(artist=self.artist, campain=campain) application.save() self.client_auth.force_authenticate(user=self.user) studentapplication_url = reverse('studentapplication-detail', kwargs={'pk': application.pk}) # update an info response = self.client_auth.patch(studentapplication_url, data={'remote_interview': 'true'}) self.assertEqual(response.status_code, status.HTTP_200_OK) # update more than one info response = self.client_auth.patch(studentapplication_url, data={'remote_interview': 'true', 'remote_interview_type': 'Skype'}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
class TestApplicationEndPoint(TestCase): """ Tests concernants le endpoint des Student Application """ def setUp(self): self.user = User() self.user.first_name = "Andrew" self.user.last_name = "Warhola" self.user.username = "******" self.user.password = "******" self.user.save() # save generate token self.token = "" self.client_auth = APIClient() self.artist = Artist(user=self.user, nickname="Andy Warhol") self.artist.save() def tearDown(self): pass def _get_list(self): url = reverse('studentapplication-list') return self.client.get(url) def _get_list_auth(self): url = reverse('studentapplication-list') return self.client_auth.get(url) def test_list(self): """ Test list of applications without authentification """ # set up a candidature application = StudentApplication(artist=self.artist) application.save() self.token = "" response = self._get_list() candidatures = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(candidatures) self.assertEqual(len(candidatures), 1) # info is NOT accessible when anonymous user self.assertRaises( KeyError, lambda: candidatures[0]['current_year_application_count']) def test_list_auth(self): """ Test list of applications with authentification """ # set up a candidature application = StudentApplication(artist=self.artist) application.save() self.client_auth.force_authenticate(user=self.user) response = self._get_list_auth() candidature = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) # info is accessible when user is auth assert candidature[0]['current_year_application_count'] is not None def test_create_student_application(self): """ Test creating an studentapplication """ self.client_auth.force_authenticate(user=self.user) studentapplication_url = reverse('studentapplication-list') response = self.client_auth.post(studentapplication_url) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(StudentApplication.objects.count(), 1) self.assertEqual( StudentApplication.objects.last().artist.user.first_name, self.user.first_name) def test_update_student_application(self): """ Test creating an studentapplication """ # set up a campaign promotion = Promotion(starting_year=2000, ending_year=2001) promotion.save() campaign = StudentApplicationSetup( candidature_date_start=timezone.now(), candidature_date_end=timezone.now() + datetime.timedelta(days=1), promotion=promotion, is_current_setup=True, ) campaign.save() # add a candidature application = StudentApplication(artist=self.artist, campaign=campaign) application.save() self.client_auth.force_authenticate(user=self.user) studentapplication_url = reverse('studentapplication-detail', kwargs={'pk': application.pk}) # update an info response = self.client_auth.patch(studentapplication_url, data={'remote_interview': 'true'}) self.assertEqual(response.status_code, status.HTTP_200_OK) # update more than one info response = self.client_auth.patch(studentapplication_url, data={ 'remote_interview': 'true', 'remote_interview_type': 'Skype' }) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)