Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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']))