class OrganizationBadgeAPITest:
    modules = ['core.user', 'core.organization']

    @pytest.fixture(autouse=True)
    def setUp(self, api, clean_db):
        # Register at least two badges
        Organization.__badges__['test-1'] = 'Test 1'
        Organization.__badges__['test-2'] = 'Test 2'

        self.factory = badge_factory(Organization)
        self.user = api.login(AdminFactory())
        self.organization = OrganizationFactory()

    def test_list(self, api):
        response = api.get(url_for('api.available_organization_badges'))
        assert200(response)
        assert len(response.json) == len(Organization.__badges__)
        for kind, label in Organization.__badges__.items():
            assert kind in response.json
            assert response.json[kind] == label

    def test_create(self, api):
        data = self.factory.as_dict()
        url = url_for('api.organization_badges', org=self.organization)
        with assert_emit(on_badge_added):
            response = api.post(url, data)
            assert201(response)
        self.organization.reload()
        assert len(self.organization.badges) is 1

    def test_create_badge_certified_mail(self, api):
        member = Member(user=self.user, role='admin')
        org = OrganizationFactory(members=[member])

        data = self.factory.as_dict()
        data['kind'] = CERTIFIED

        with capture_mails() as mails:
            api.post(url_for('api.organization_badges', org=org), data)

        # Should have sent one mail to each member of organization
        members_emails = [m.user.email for m in org.members]
        assert len(mails) == len(members_emails)
        assert [m.recipients[0] for m in mails] == members_emails

    def test_create_badge_public_service_mail(self, api):
        member = Member(user=self.user, role='admin')
        org = OrganizationFactory(members=[member])

        data = self.factory.as_dict()
        data['kind'] = PUBLIC_SERVICE

        with capture_mails() as mails:
            api.post(url_for('api.organization_badges', org=org), data)
            # do it a second time, no email expected for this one
            api.post(url_for('api.organization_badges', org=self.organization),
                     data)

        # Should have sent one mail to each member of organization
        members_emails = [m.user.email for m in org.members]
        assert len(mails) == len(members_emails)
        assert [m.recipients[0] for m in mails] == members_emails

    def test_create_same(self, api):
        data = self.factory.as_dict()
        url = url_for('api.organization_badges', org=self.organization)
        with assert_emit(on_badge_added):
            api.post(url, data)
        with assert_not_emit(on_badge_added):
            response = api.post(url, data)
            assert200(response)
        self.organization.reload()
        assert len(self.organization.badges) is 1

    def test_create_2nd(self, api):
        # Explicitely setting the kind to avoid collisions given the
        # small number of choices for kinds.
        kinds_keys = Organization.__badges__.keys()
        self.organization.add_badge(kinds_keys[0])
        data = self.factory.as_dict()
        data['kind'] = kinds_keys[1]
        url = url_for('api.organization_badges', org=self.organization)
        response = api.post(url, data)
        assert201(response)
        self.organization.reload()
        assert len(self.organization.badges) is 2

    def test_delete(self, api):
        badge = self.factory()
        self.organization.add_badge(badge.kind)
        self.organization.save()
        url = url_for('api.organization_badge',
                      org=self.organization,
                      badge_kind=str(badge.kind))
        with assert_emit(on_badge_removed):
            response = api.delete(url)
            assert204(response)
        self.organization.reload()
        assert len(self.organization.badges) is 0

    def test_delete_404(self, api):
        kind = str(self.factory().kind)
        url = url_for('api.organization_badge',
                      org=self.organization,
                      badge_kind=kind)
        response = api.delete(url)
        assert404(response)
Esempio n. 2
0
class OrganizationBadgeAPITest:
    modules = []

    @pytest.fixture(autouse=True)
    def setUp(self, api, clean_db):
        # Register at least two badges
        Organization.__badges__['test-1'] = 'Test 1'
        Organization.__badges__['test-2'] = 'Test 2'

        self.factory = badge_factory(Organization)
        self.user = api.login(AdminFactory())
        self.organization = OrganizationFactory()

    def test_list(self, api):
        response = api.get(url_for('api.available_organization_badges'))
        assert200(response)
        assert len(response.json) == len(Organization.__badges__)
        for kind, label in Organization.__badges__.items():
            assert kind in response.json
            assert response.json[kind] == label

    def test_create(self, api):
        data = self.factory.as_dict()
        url = url_for('api.organization_badges', org=self.organization)
        with assert_emit(on_badge_added):
            response = api.post(url, data)
            assert201(response)
        self.organization.reload()
        assert len(self.organization.badges) is 1

    def test_create_same(self, api):
        data = self.factory.as_dict()
        url = url_for('api.organization_badges', org=self.organization)
        with assert_emit(on_badge_added):
            api.post(url, data)
        with assert_not_emit(on_badge_added):
            response = api.post(url, data)
            assert200(response)
        self.organization.reload()
        assert len(self.organization.badges) is 1

    def test_create_2nd(self, api):
        # Explicitely setting the kind to avoid collisions given the
        # small number of choices for kinds.
        kinds_keys = list(Organization.__badges__)
        self.organization.add_badge(kinds_keys[0])
        data = self.factory.as_dict()
        data['kind'] = kinds_keys[1]
        url = url_for('api.organization_badges', org=self.organization)
        response = api.post(url, data)
        assert201(response)
        self.organization.reload()
        assert len(self.organization.badges) is 2

    def test_delete(self, api):
        badge = self.factory()
        self.organization.add_badge(badge.kind)
        self.organization.save()
        url = url_for('api.organization_badge',
                      org=self.organization,
                      badge_kind=str(badge.kind))
        with assert_emit(on_badge_removed):
            response = api.delete(url)
            assert204(response)
        self.organization.reload()
        assert len(self.organization.badges) is 0

    def test_delete_404(self, api):
        kind = str(self.factory().kind)
        url = url_for('api.organization_badge', org=self.organization, badge_kind=kind)
        response = api.delete(url)
        assert404(response)