def test_suggestion_project_link(self): SuggestionFactory.create(title="Adoptable", destination="Amsterdam", status="accepted", project=None, token='x', org_name='Acme Inc.', org_email='*****@*****.**', org_website='http://example.com', org_phone='123123123', org_contactname='John Doe', pitch='Eat more cheese', language='en') response = self.client.get(self.suggestion_list_url, { 'destination': "Amsterdam", status: "accepted" }, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content)[0] project = ProjectFactory.create(title="Adopting project") data['project'] = project.slug self.client.put(reverse('suggestion_detail', kwargs={'pk': data['id']}), HTTP_AUTHORIZATION=self.user_1_token, data=data) suggestion = Suggestion.objects.get(pk=data['id']) self.assertEquals(Project.objects.get(pk=project.pk), suggestion.project)
def test_suggestion_project_link(self): SuggestionFactory.create(title="Adoptable", destination="Amsterdam", status="accepted", project=None, token='x', org_name='Acme Inc.', org_email='*****@*****.**', org_website='http://example.com', org_phone='123123123', org_contactname='John Doe', pitch='Eat more cheese' ) response = self.client.get(self.suggestion_list_url, {'destination': "Amsterdam", status: "accepted"}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content)[0] project = ProjectFactory.create(title="Adopting project") data['project'] = project.slug r = self.client.put(reverse('suggestion_detail', kwargs={'pk':data['id']}), HTTP_AUTHORIZATION=self.user_1_token, data=data) suggestion = Suggestion.objects.get(pk=data['id']) self.assertEquals(Project.objects.get(pk=project.pk), suggestion.project)
def test_retrieve_suggestion_list_all_items(self): """ Test if all suggestions are retrieved that have a deadline today or later """ suggestion_2 = SuggestionFactory.create(deadline=date.today()) suggestion_3 = SuggestionFactory.create(deadline=date.today()) response = self.client.get(self.suggestion_list_url, HTTP_AUTHORIZATION=self.user_1_token) self.assertEqual(response.status_code, status.HTTP_200_OK) data = json.loads(response.content) self.assertEqual(len(data), Suggestion.objects.count())
def test_retrieve_suggestion_list_all_items(self): """ Test if all suggestions are retrieved that have a deadline today or later """ SuggestionFactory.create(deadline=date.today()) SuggestionFactory.create(deadline=date.today()) response = self.client.get(self.suggestion_list_url, HTTP_AUTHORIZATION=self.user_1_token) self.assertEqual(response.status_code, status.HTTP_200_OK) data = json.loads(response.content) self.assertEqual(len(data), Suggestion.objects.count())
def test_retrieve_suggestion_list_expired_deadlines(self): """ Test that no suggestions are returned if there deadline is 'lower' than today """ suggestion_2 = SuggestionFactory.create(deadline=date.today() - timedelta(1)) suggestion_3 = SuggestionFactory.create(deadline=date.today() - timedelta(1)) response = self.client.get(self.suggestion_list_url, HTTP_AUTHORIZATION=self.user_1_token) self.assertEqual(response.status_code, status.HTTP_200_OK) data = json.loads(response.content) # We still have the original initialized Suggestion, so 1 instead of 0 self.assertEqual(len(data), 1)
def test_retrieve_no_suggestions_with_fake_project_slug(self): """ Test the project slug filter on the API endpoint. Shouldn't return any suggestions """ project = ProjectFactory.create() SuggestionFactory.create(project=project) response = self.client.get(self.suggestion_list_url, {'project_slug': "non-existing-slug"}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 0)
def test_suggestions_states(self): """ verify all other states / cases are counted properly """ states = ("unconfirmed", "draft", "accepted", "rejected") for state in states: SuggestionFactory.create(title="Suggestion with state " + state, deadline=self.future, status=state) suggestion_metrics = self.metrics.calculate_suggestion_metrics() for state in states: self.assertEqual(suggestion_metrics[state], 1)
def test_retrieve_suggestion_list_expired_deadlines(self): """ Test that no suggestions are returned if there deadline is 'lower' than today """ SuggestionFactory.create(deadline=date.today() - timedelta(1)) SuggestionFactory.create(deadline=date.today() - timedelta(1)) response = self.client.get(self.suggestion_list_url, HTTP_AUTHORIZATION=self.user_1_token) self.assertEqual(response.status_code, status.HTTP_200_OK) data = json.loads(response.content) # We still have the original initialized Suggestion, so 1 instead of 0 self.assertEqual(len(data), 1)
def test_retrieve_only_suggestions_with_project_slug(self): """ Test the project slug filter on the API endpoint. Should return one suggestion """ project = ProjectFactory.create() SuggestionFactory.create(project=project) response = self.client.get(self.suggestion_list_url, {'project_slug': project.slug}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 1) self.assertEqual(data[0]['project'].lower(), project.slug)
def setUp(self): super(SuggestionsListIntegrationTest, self).setUp() self.user_1 = BlueBottleUserFactory.create() self.user_1_token = "JWT {0}".format(self.user_1.get_jwt_token()) self.init_projects() self.suggestion = SuggestionFactory.create() self.suggestion_list_url = "/api/suggestions/"
def test_retrieve_only_suggestions_with_destination(self): """ Test the destination filter on the API endpoint. """ destination = 'amsterdam' suggestion_amsterdam_1 = SuggestionFactory.create(destination="Amsterdam") suggestion_amsterdam_2 = SuggestionFactory.create(destination="amsterdam") response = self.client.get(self.suggestion_list_url, {'destination': destination}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 2) self.assertEqual(data[0]['destination'].lower(), destination) self.assertEqual(data[1]['destination'].lower(), destination)
def test_retrieve_only_suggestions_with_status(self): """ Test the status filter on the API endpoint. """ status = 'accepted' suggestion_accepted_1 = SuggestionFactory.create(status=status) suggestion_accepted_2 = SuggestionFactory.create(status=status) suggestion_other = SuggestionFactory.create(status='other') response = self.client.get(self.suggestion_list_url, {'status': status}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 2) self.assertEqual(data[0]['status'].lower(), status) self.assertEqual(data[1]['status'].lower(), status)
def test_retrieve_only_suggestions_with_destination(self): """ Test the destination filter on the API endpoint. """ destination = 'amsterdam' SuggestionFactory.create(destination="Amsterdam") SuggestionFactory.create(destination="amsterdam") response = self.client.get(self.suggestion_list_url, {'destination': destination}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 2) self.assertEqual(data[0]['destination'].lower(), destination) self.assertEqual(data[1]['destination'].lower(), destination)
def test_suggestions_expired(self): """ verify suggestions past the deadline are expired """ yesterday = datetime.today() - timedelta(days=1) not1 = SuggestionFactory.create(title="expired suggestion", deadline=yesterday, status="submitted") suggestion_metrics = self.metrics.calculate_suggestion_metrics() self.assertEqual(suggestion_metrics['expired'], 1)
def test_suggestions_today_not_expired(self): """ if the deadline is today, there's still time """ today = datetime.today() not1 = SuggestionFactory.create(title="expired suggestion", deadline=today, status="submitted") suggestion_metrics = self.metrics.calculate_suggestion_metrics() self.assertEqual(suggestion_metrics['expired'], 0)
def test_suggestions_adopted(self): """ a submitted suggestion with project is adopted, all others aren't """ p = ProjectFactory() not1 = SuggestionFactory.create(title="wrong state", deadline=self.future, project=p, status="in_progress") not2 = SuggestionFactory.create(title="no project", project=None, deadline=self.future, status="submitted") adopt = SuggestionFactory.create(title="adopted", deadline=self.future, project=p, status="submitted") suggestion_metrics = self.metrics.calculate_suggestion_metrics() self.assertEqual(suggestion_metrics['adopted'], 1)
def test_retrieve_no_suggestions_with_fake_project_slug(self): """ Test the project slug filter on the API endpoint. Shouldn't return any suggestions """ project = ProjectFactory.create() suggestion_accepted_1 = SuggestionFactory.create(project=project) response = self.client.get(self.suggestion_list_url, {'project_slug': "non-existing-slug"}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 0)
def test_token_validate_unauthorized(self): """ validate a suggesting by its token (authorized) """ token = str(uuid.uuid4()) suggestion = SuggestionFactory.create(token=token, status='unconfirmed') response = self.client.put( reverse('suggestion_token_validate', kwargs={'token':token})) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) suggestion = Suggestion.objects.get(pk=suggestion.pk) self.assertEquals(suggestion.status, 'unconfirmed')
def test_token_validate_already_validated(self): """ validate a suggesting by its token (authorized) """ token = str(uuid.uuid4()) suggestion = SuggestionFactory.create(token=token, status='draft') response = self.client.put( reverse('suggestion_token_validate', kwargs={'token':token}), HTTP_AUTHORIZATION=self.user_1_token) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) suggestion = Suggestion.objects.get(pk=suggestion.pk) self.assertEquals(suggestion.status, 'draft')
def test_retrieve_only_suggestions_with_project_slug(self): """ Test the project slug filter on the API endpoint. Should return one suggestion """ project = ProjectFactory.create() suggestion_accepted_1 = SuggestionFactory.create(project=project) response = self.client.get(self.suggestion_list_url, {'project_slug': project.slug}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 1) self.assertEqual(data[0]['project'].lower(), project.slug)
def test_token_validate_already_validated(self): """ validate a suggesting by its token (authorized) """ token = str(uuid.uuid4()) suggestion = SuggestionFactory.create(token=token, status='draft') response = self.client.put(reverse('suggestion_token_validate', kwargs={'token': token}), HTTP_AUTHORIZATION=self.user_1_token) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) suggestion = Suggestion.objects.get(pk=suggestion.pk) self.assertEquals(suggestion.status, 'draft')
def test_project_submitted_suggestion_submitted(self): """ Test that suggestion has status submitted if a project status changes to submitted """ project = ProjectFactory.create(status=self.new) suggestion = SuggestionFactory.create(project=project, token='xxx', status='in_progress') project.status = self.submitted project.save() suggestion = Suggestion.objects.get(project=project) self.assertEquals(suggestion.status, 'submitted')
def test_project_needs_work_suggestion_in_progress(self): """ Test that suggestion has status in-progress if a project status changes to needs-work """ project = ProjectFactory.create(status=self.submitted) suggestion = SuggestionFactory.create(project=project, token='xxx', status='submitted') project.status = self.needs_work project.save() suggestion = Suggestion.objects.get(project=project) self.assertEquals(suggestion.status, 'in_progress')
def test_retrieve_only_suggestions_with_status(self): """ Test the status filter on the API endpoint. """ accepted = 'accepted' SuggestionFactory.create(status=accepted) SuggestionFactory.create(status=accepted) SuggestionFactory.create(status='other') response = self.client.get(self.suggestion_list_url, {'status': accepted}, HTTP_AUTHORIZATION=self.user_1_token) data = json.loads(response.content) self.assertEqual(len(data), 2) self.assertEqual(data[0]['status'].lower(), accepted) self.assertEqual(data[1]['status'].lower(), accepted)