def test_filter_not_owner(self): """ Non-owner should only see approved initiatives """ InitiativeFactory.create_batch(2, status='submitted', owner=self.owner) InitiativeFactory.create_batch(4, status='approved', owner=self.owner) InitiativeFactory.create_batch(3, status='approved') response = self.client.get( self.url + '?filter[owner.id]={}'.format(self.owner.pk), user=self.visitor) data = json.loads(response.content) self.assertEqual(data['meta']['pagination']['count'], 4) self.assertEqual( data['data'][0]['relationships']['owner']['data']['id'], str(self.owner.pk))
def test_sort_matching_combined(self): theme = ProjectThemeFactory.create() self.owner.favourite_themes.add(theme) skill = SkillFactory.create() self.owner.skills.add(skill) self.owner.location = LocationFactory.create(position='10.0, 20.0') self.owner.save() initiative = InitiativeFactory.create(theme=theme) first = EventFactory.create(status='open', initiative=initiative, is_online=False) second = AssignmentFactory.create( status='open', location=GeolocationFactory.create(position=Point(21.0, 9.0)), initiative=initiative, is_online=False) third = AssignmentFactory.create( status='open', location=GeolocationFactory.create(position=Point(21.0, 9.0)), initiative=initiative, expertise=skill, is_online=False) response = self.client.get(self.url + '?sort=popularity', user=self.owner) data = json.loads(response.content) self.assertEqual(data['meta']['pagination']['count'], 3) self.assertEqual(data['data'][0]['id'], str(third.pk)) self.assertEqual(data['data'][1]['id'], str(second.pk)) self.assertEqual(data['data'][2]['id'], str(first.pk))
def setUp(self): super(AssignmentDetailApplicantsAPITestCase, self).setUp() self.settings = InitiativePlatformSettingsFactory.create( activity_types=['assignment']) self.user = BlueBottleUserFactory() self.owner = BlueBottleUserFactory() self.initiative = InitiativeFactory(owner=self.user) self.assignment = AssignmentFactory.create(initiative=self.initiative, status='open', owner=self.owner) self.client = JSONAPITestClient() self.url = reverse('assignment-detail', args=(self.assignment.id, )) ApplicantFactory.create_batch(5, activity=self.assignment, status='accepted') ApplicantFactory.create_batch(3, activity=self.assignment, status='new') ApplicantFactory.create_batch(2, activity=self.assignment, status='rejected')
def setUp(self): super(FundingTestCase, self).setUp() user = BlueBottleUserFactory.create(first_name='Jean Baptiste') self.initiative = InitiativeFactory.create(activity_manager=user) self.initiative.states.submit() self.initiative.states.approve(save=True) payout_account = StripePayoutAccountFactory.create(status='verified') bank_account = ExternalAccountFactory.create( connect_account=payout_account, status='verified') self.funding = FundingFactory.create(owner=user, initiative=self.initiative, target=Money(500, 'EUR'), deadline=now() + timedelta(weeks=2), bank_account=bank_account) BudgetLineFactory.create(activity=self.funding) self.funding.bank_account.reviewed = True self.funding.states.submit() self.funding.states.approve(save=True) BudgetLineFactory.create_batch(4, activity=self.funding, amount=Money(125, 'EUR')) mail.outbox = []
def test_search(self): first = InitiativeFactory.create(title='Lorem ipsum dolor sit amet', pitch="Lorem ipsum", status='approved') InitiativeFactory.create(title='consectetur adipiscing elit', status='approved') InitiativeFactory.create(title='Nam eu turpis erat', status='approved') second = InitiativeFactory.create(title='Lorem ipsum dolor sit amet', status='approved') response = self.client.get(self.url + '?filter[search]=lorem ipsum', HTTP_AUTHORIZATION="JWT {0}".format( self.owner.get_jwt_token())) data = json.loads(response.content) self.assertEqual(data['meta']['pagination']['count'], 2) self.assertEqual(data['data'][0]['id'], str(second.pk)) self.assertEqual(data['data'][1]['id'], str(first.pk))
def test_slug_special_characters(self): initiative = InitiativeFactory(title='!!! $$$$') self.assertEqual(initiative.slug, 'new')
class InitiativeDetailAPITestCase(InitiativeAPITestCase): def setUp(self): super(InitiativeDetailAPITestCase, self).setUp() self.initiative = InitiativeFactory( owner=self.owner, place=GeolocationFactory(position=Point(23.6851594, 43.0579025))) self.initiative.states.submit(save=True) self.url = reverse('initiative-detail', args=(self.initiative.pk, )) self.data = { 'data': { 'id': self.initiative.id, 'type': 'initiatives', 'attributes': { 'title': 'Some title' } } } def test_patch(self): response = self.client.patch(self.url, json.dumps(self.data), user=self.owner) self.assertEqual(response.status_code, status.HTTP_200_OK) data = json.loads(response.content) self.assertEqual(data['data']['attributes']['title'], 'Some title') def test_put_image(self): file_path = './bluebottle/files/tests/files/test-image.png' with open(file_path, 'rb') as test_file: response = self.client.post( reverse('image-list'), test_file.read(), content_type="image/png", HTTP_CONTENT_DISPOSITION='attachment; filename="some_file.png"', user=self.owner) file_data = json.loads(response.content) data = { 'data': { 'id': self.initiative.id, 'type': 'initiatives', 'relationships': { 'image': { 'data': { 'type': 'images', 'id': file_data['data']['id'] } } } } } response = self.client.patch(self.url, json.dumps(data), content_type="application/vnd.api+json", user=self.owner) self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() self.assertEqual(data['data']['relationships']['image']['data']['id'], file_data['data']['id']) image = get_include(response, 'images') response = self.client.get(image['attributes']['links']['large'], user=self.owner) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue( response['X-Accel-Redirect'].startswith('/media/cache/')) def test_put_location(self): location = LocationFactory.create() data = { 'data': { 'id': self.initiative.id, 'type': 'initiatives', 'relationships': { 'location': { 'data': { 'type': 'locations', 'id': location.pk } } } } } response = self.client.patch(self.url, json.dumps(data), content_type="application/vnd.api+json", HTTP_AUTHORIZATION="JWT {0}".format( self.owner.get_jwt_token())) self.assertEqual(response.status_code, 200) data = json.loads(response.content) self.assertEqual( data['data']['relationships']['location']['data']['id'], str(location.pk)) self.assertEqual( get_include(response, 'locations')['attributes']['name'], location.name) def test_patch_anonymous(self): response = self.client.patch( self.url, json.dumps(self.data), ) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_patch_wrong_user(self): response = self.client.patch(self.url, json.dumps(self.data), user=BlueBottleUserFactory.create()) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_update_cancelled(self): self.initiative.states.approve() self.initiative.states.cancel(save=True) response = self.client.put(self.url, json.dumps(self.data), user=self.initiative.owner) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_update_deleted(self): self.initiative = InitiativeFactory.create() self.initiative.states.delete(save=True) response = self.client.put(self.url, json.dumps(self.data), user=self.initiative.owner) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_update_rejected(self): self.initiative = InitiativeFactory.create() self.initiative.states.reject(save=True) response = self.client.put(self.url, json.dumps(self.data), user=self.initiative.owner) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_get_owner(self): self.initiative.title = '' self.initiative.save() response = self.client.get(self.url, user=self.owner) data = response.json()['data'] self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['title'], self.initiative.title) self.assertEqual(data['meta']['status'], self.initiative.status) self.assertEqual(data['meta']['transitions'], [{ u'available': True, u'name': u'request_changes', u'target': u'needs_work' }]) self.assertEqual(data['relationships']['theme']['data']['id'], str(self.initiative.theme.pk)) self.assertEqual(data['relationships']['owner']['data']['id'], str(self.initiative.owner.pk)) geolocation = get_include(response, 'geolocations') self.assertEqual(geolocation['attributes']['position'], { 'latitude': 43.0579025, 'longitude': 23.6851594 }) self.assertTrue('/data/attributes/title' in ( error['source']['pointer'] for error in data['meta']['required'])) def test_get_activities(self): event = EventFactory.create(initiative=self.initiative, image=ImageFactory.create()) response = self.client.get(self.url, user=self.owner) data = response.json()['data'] self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(data['relationships']['activities']['data']), 1) self.assertEqual(data['relationships']['activities']['data'][0]['id'], str(event.pk)) self.assertEqual( data['relationships']['activities']['data'][0]['type'], 'activities/events') activity_data = get_include(response, 'activities/events') self.assertEqual(activity_data['attributes']['title'], event.title) self.assertEqual(activity_data['type'], 'activities/events') activity_location = activity_data['relationships']['location']['data'] self.assertTrue(activity_location in ({ 'type': included['type'], 'id': included['id'] } for included in response.json()['included'])) activity_image = activity_data['relationships']['image']['data'] self.assertTrue(activity_image in ({ 'type': included['type'], 'id': included['id'] } for included in response.json()['included'])) def test_deleted_activities(self): EventFactory.create(initiative=self.initiative, status='deleted') response = self.client.get(self.url, user=self.owner) data = response.json()['data'] self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(data['relationships']['activities']['data']), 0) def test_get_stats(self): event = EventFactory.create(initiative=self.initiative, status='succeeded') ParticipantFactory.create_batch(3, activity=event, status='succeeded', time_spent=3) assignment = AssignmentFactory.create(initiative=self.initiative, status='succeeded', duration=3) ApplicantFactory.create_batch(3, activity=assignment, status='succeeded', time_spent=3) funding = FundingFactory.create(initiative=self.initiative, status='succeeded') DonationFactory.create_batch(3, activity=funding, status='succeeded', amount=Money(10, 'EUR')) DonationFactory.create_batch(3, activity=funding, status='succeeded', amount=Money(10, 'USD')) response = self.client.get(self.url, user=self.owner) self.assertEqual(response.status_code, status.HTTP_200_OK) stats = response.json()['data']['meta']['stats'] self.assertEqual(stats['hours'], 18) self.assertEqual(stats['activities'], 3) self.assertEqual(stats['contributions'], 12) self.assertEqual(stats['amount'], 75.0) def test_get_other(self): response = self.client.get(self.url, user=BlueBottleUserFactory.create()) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['title'], self.initiative.title) def test_get_anonymous(self): response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['title'], self.initiative.title)
def test_story_safe(self): initiative = InitiativeFactory(story='<img src="test">') self.assertEqual(initiative.story, '<img src="test">')
def test_slug_empty(self): initiative = InitiativeFactory(title='') self.assertEqual(initiative.slug, 'new')
def test_organization_not_required(self): InitiativePlatformSettingsFactory.create(require_organization=False) initiative = InitiativeFactory() self.assertFalse(initiative.has_organization)
def test_slug(self): initiative = InitiativeFactory(title='Test Title') self.assertEqual(initiative.slug, 'test-title')
class AssignmentCreateAPITestCase(BluebottleTestCase): def setUp(self): super(AssignmentCreateAPITestCase, self).setUp() self.settings = InitiativePlatformSettingsFactory.create( activity_types=['assignment']) self.client = JSONAPITestClient() self.url = reverse('assignment-list') self.user = BlueBottleUserFactory() self.initiative = InitiativeFactory(owner=self.user) self.initiative.states.submit() self.initiative.states.approve(save=True) self.initiative.save() def test_create_assignment(self): skill = SkillFactory.create() data = { 'data': { 'type': 'activities/assignments', 'attributes': { 'title': 'Business plan Young Freddy', 'date': str((timezone.now() + timedelta(days=21))), 'end_date_type': 'deadline', 'duration': 8, 'registration_deadline': str((timezone.now() + timedelta(days=14)).date()), 'capacity': 2, 'description': 'Help Young Freddy write a business plan', 'is-online': True, }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, 'expertise': { 'data': { 'type': 'skills', 'id': skill.pk }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data['status'], 'draft') self.assertEqual(response.data['title'], 'Business plan Young Freddy') def test_create_assignment_missing_data(self): data = { 'data': { 'type': 'activities/assignments', 'attributes': { 'title': '', 'date': str((timezone.now() + timedelta(days=21))), 'registration_deadline': str((timezone.now() + timedelta(days=14)).date()), 'description': 'Help Young Freddy write a business plan' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue('/data/attributes/title' in ( error['source']['pointer'] for error in response.json()['data']['meta']['required'])) self.assertTrue('/data/attributes/end-date-type' in ( error['source']['pointer'] for error in response.json()['data']['meta']['required'])) self.assertTrue('/data/attributes/is-online' in ( error['source']['pointer'] for error in response.json()['data']['meta']['required'])) def test_create_registration_deadline(self): data = { 'data': { 'type': 'activities/assignments', 'attributes': { 'title': '', 'date': str((timezone.now() + timedelta(days=21))), 'registration_deadline': str((timezone.now() + timedelta(days=22)).date()), 'description': 'Help Young Freddy write a business plan' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue('/data/attributes/registration-deadline' in ( error['source']['pointer'] for error in response.json()['data']['meta']['errors']))
def setUp(self): super(TestThemeAdmin, self).setUp() self.site = AdminSite() self.skill_admin = ProjectThemeAdmin(Skill, self.site) self.client.force_login(self.superuser) InitiativeFactory.create()
def test_follow_without_object(self): initiative = InitiativeFactory.create() follow = FollowFactory.create(followed_object=initiative) initiative.delete() title = self.follow_admin.title(follow) self.assertEqual(title, '-')
def test_submit_unsubmitted_initiative(self): initiative = InitiativeFactory.create() event = EventFactory.create(initiative=initiative) self.assertRaises(TransitionNotPossible, event.states.submit)
def test_follow_object_title(self): initiative = InitiativeFactory.create() follow = FollowFactory.create(followed_object=initiative) title = self.follow_admin.title(follow) self.assertEqual(initiative.title, title)
def test_translated_messages(self): dutch = BlueBottleUserFactory.create(primary_language='nl') message = TestMessage(InitiativeFactory(owner=dutch, title='Some title')) messages = list(message.get_messages()) self.assertEqual(len(messages), 1) self.assertEqual(messages[0].subject, "Test bericht voor Some title")
def test_message_subject_without_context(self): english = BlueBottleUserFactory.create(primary_language='en') message = AnotherTestMessage(InitiativeFactory(title='Some title', owner=english)) messages = list(message.get_messages()) self.assertEqual(messages[0].subject, "Test message")
def setUp(self): super(AssignmentTransitionTestCase, self).setUp() self.client = JSONAPITestClient() self.owner = BlueBottleUserFactory() self.manager = BlueBottleUserFactory() self.other_user = BlueBottleUserFactory() self.initiative = InitiativeFactory.create( activity_manager=self.manager) self.initiative.states.submit() self.initiative.states.approve(save=True) self.initiative = InitiativeFactory.create( activity_manager=self.manager) self.initiative.states.submit() self.initiative.states.approve(save=True) self.assignment_incomplete = AssignmentFactory.create( owner=self.owner, initiative=self.initiative, is_online=None, duration=None, end_date_type=None, date=None) self.assignment = AssignmentFactory.create(owner=self.owner, initiative=self.initiative) self.assignment_incomplete_url = reverse( 'assignment-detail', args=(self.assignment_incomplete.id, )) self.assignment_url = reverse('assignment-detail', args=(self.assignment.id, )) self.transition_url = reverse('assignment-transition-list') self.review_data = { 'data': { 'type': 'assignment-transitions', 'attributes': { 'transition': 'submit', }, 'relationships': { 'resource': { 'data': { 'type': 'activities/assignments', 'id': self.assignment_incomplete.pk } } } } } self.transition_data = { 'data': { 'type': 'assignment-transitions', 'attributes': { 'transition': 'cancel', }, 'relationships': { 'resource': { 'data': { 'type': 'activities/assignments', 'id': self.assignment.pk } } } } }
def test_member_preselect_organization(self): member = BlueBottleUserFactory.create( partner_organization=OrganizationFactory.create()) initiative = InitiativeFactory(owner=member, has_organization=None) self.assertEqual(initiative.organization, member.partner_organization)
def test_activity_manager(self): initiative = InitiativeFactory(activity_manager=None) self.assertEqual(initiative.owner, initiative.activity_manager)
class EventListAPITestCase(BluebottleTestCase): def setUp(self): super(EventListAPITestCase, self).setUp() self.settings = InitiativePlatformSettingsFactory.create( activity_types=['event']) self.client = JSONAPITestClient() self.url = reverse('event-list') self.user = BlueBottleUserFactory() self.initiative = InitiativeFactory(owner=self.user) self.initiative.states.submit(save=True) def test_create_event_complete(self): start = now() + timedelta(days=21) data = { 'data': { 'type': 'activities/events', 'attributes': { 'title': 'Beach clean-up Katwijk', 'start': str(start), 'duration': 4, 'is_online': True, 'online_meeting_url': 'https://example.com', 'registration_deadline': str((now() + timedelta(days=14)).date()), 'capacity': 10, 'description': 'We will clean up the beach south of Katwijk' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data['status'], 'draft') self.assertEqual(response.data['title'], 'Beach clean-up Katwijk') self.assertEqual(response.data['online_meeting_url'], 'https://example.com') self.assertEqual( { transition['name'] for transition in response.json()['data']['meta'] ['transitions'] }, {'submit', 'delete'}) # Add an event with the same title should NOT return an error response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) def test_create_event_disabled(self): self.settings.activity_types = ('funding', ) self.settings.save() start = now() + timedelta(days=21) data = { 'data': { 'type': 'activities/events', 'attributes': { 'title': 'Beach clean-up Katwijk', 'start': str(start), 'duration': 4, 'registration_deadline': str((now() + timedelta(days=14)).date()), 'capacity': 10, 'address': 'Zuid-Boulevard Katwijk aan Zee', 'description': 'We will clean up the beach south of Katwijk' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_create_event_no_title(self): start = now() + timedelta(days=21) data = { 'data': { 'type': 'activities/events', 'attributes': { 'start': str(start), 'duration': 4, 'registration_deadline': str((now() + timedelta(days=14)).date()), 'capacity': 10, 'address': 'Zuid-Boulevard Katwijk aan Zee', 'description': 'We will clean up the beach south of Katwijk' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue('/data/attributes/title' in ( error['source']['pointer'] for error in response.json()['data']['meta']['required'])) def test_create_event_no_location(self): start = now() + timedelta(days=21) data = { 'data': { 'type': 'activities/events', 'attributes': { 'start': str(start), 'is_online': False, 'duration': 4, 'registration_deadline': str((now() + timedelta(days=14)).date()), 'capacity': 10, 'description': 'We will clean up the beach south of Katwijk' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue('/data/attributes/location' in ( error['source']['pointer'] for error in response.json()['data']['meta']['required'])) def test_create_event_no_location_is_online(self): start = now() + timedelta(days=21) data = { 'data': { 'type': 'activities/events', 'attributes': { 'start': str(start), 'is_online': True, 'duration': 4, 'registration_deadline': str((now() + timedelta(days=14)).date()), 'capacity': 10, 'description': 'We will clean up the beach south of Katwijk' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=self.user) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue('/data/attributes/location' not in ( error['source']['pointer'] for error in response.json()['data']['meta']['errors'])) def test_create_event_as_activity_manager(self): activity_manager = BlueBottleUserFactory.create() self.initiative.activity_manager = activity_manager self.initiative.save() start = now() + timedelta(days=21) registration = now() + timedelta(days=14) data = { 'data': { 'type': 'activities/events', 'attributes': { 'title': 'Beach clean-up Katwijk', 'start': str(start), 'registration_deadline': str(registration.date()), 'capacity': 10, 'description': 'We will clean up the beach south of Katwijk' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=activity_manager) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data['status'], 'draft') self.assertEqual(response.data['title'], 'Beach clean-up Katwijk') def test_create_event_not_initiator(self): another_user = BlueBottleUserFactory.create() data = { 'data': { 'type': 'activities/events', 'attributes': { 'title': 'Beach clean-up Katwijk', 'start': str((now() + timedelta(days=21))), 'registration_deadline': str((now() + timedelta(days=14)).date()), 'capacity': 10, 'address': 'Zuid-Boulevard Katwijk aan Zee', 'description': 'We will clean up the beach south of Katwijk' }, 'relationships': { 'initiative': { 'data': { 'type': 'initiatives', 'id': self.initiative.id }, }, } } } response = self.client.post(self.url, json.dumps(data), user=another_user) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_absolute_url(self): initiative = InitiativeFactory(activity_manager=None) expected = 'http://testserver/en/initiatives/details/{}/{}'.format( initiative.id, initiative.slug) self.assertEqual(initiative.get_absolute_url(), expected)
def setUp(self): self.permission = OneOf(ResourceOwnerPermission, ResourcePermission)() self.user = BlueBottleUserFactory.create() self.initiative = InitiativeFactory.create(owner=self.user) self.user.groups.clear()