Ejemplo n.º 1
0
    def setUp(self):
        super(DmarcDnsTestsMixin, self).setUp()

        self.dmarc_txt_records = {}

        self._dmarc_spy_agency = SpyAgency()
        self._dmarc_spy_agency.spy_on(dns_resolver.query,
                                      call_fake=self._dns_query)
        cache.clear()
Ejemplo n.º 2
0
def spy_agency():
    """Provide a KGB spy agency to a Pytest unit test.

    Yields:
        kgb.SpyAgency:
        The spy agency.
    """
    agency = SpyAgency()

    try:
        yield agency
    finally:
        agency.unspy_all()
class SignupStatusCodeTestCase(TestCase):
    fixtures = ['invites.json', 'members.json', 'swots.json', 'users.json']
    spy_agent = SpyAgency()

    def test_successful_signup_users_with_pending_invites_should_create_memberships(
            self):
        email = '*****@*****.**'
        username = '******'
        password = '******'
        create_membership = self.spy_agent.spy_on(
            SwotMember.objects.create,
            call_original=False,
        )

        client.post(reverse('authenticationjwt:signup'),
                    content_type='application/json',
                    data=json.dumps({
                        'user': {
                            'email': email,
                            'username': username,
                            'password': password,
                        }
                    }))

        n = len(Invite.objects.filter(email=email))
        self.assertEqual(len(create_membership.calls), n)
Ejemplo n.º 4
0
    def setUp(self):
        super(DmarcDnsTestsMixin, self).setUp()

        self.dmarc_txt_records = {}

        self._dmarc_spy_agency = SpyAgency()
        self._dmarc_spy_agency.spy_on(dns_resolver.query,
                                      call_fake=self._dns_query)
        cache.clear()
Ejemplo n.º 5
0
    def test_post_vote_should_get_score_from_wilson_confidence(self):
        swot_item_id = 11
        confidence_spy = SpyAgency().spy_on(sorting.confidence,
                                            call_original=True)

        client.post(
            reverse('swot_item_vote:post',
                    kwargs={'swot_item_id': swot_item_id}),
            data=json.dumps(self.vote_up),
            content_type='application/json',
        )

        self.assertTrue(confidence_spy.called)
Ejemplo n.º 6
0
class DmarcDnsTestsMixin(object):
    """Mixin to help with e-mail tests that need to perform DMARC lookups.

    This mixin makes it easy for unit tests to fake DMARC results, in order
    to prevent the need from looking up real data from real records.

    Unit tests can provide a mapping from domain names to TXT record strings
    in the :py:attr:`dmarc_txt_records` dictionary. When a DMARC lookup is
    performed, this dictionary will be used for the lookup.

    Note that this mixin will also clear the memory cache before each test
    run.

    Attributes:
        dmarc_txt_records (dict):
            A dictionary of domain names to DMARC TXT record strings.
    """

    def setUp(self):
        super(DmarcDnsTestsMixin, self).setUp()

        self.dmarc_txt_records = {}

        self._dmarc_spy_agency = SpyAgency()
        self._dmarc_spy_agency.spy_on(dns_resolver.query,
                                      call_fake=self._dns_query)
        cache.clear()

    def tearDown(self):
        super(DmarcDnsTestsMixin, self).tearDown()

        self._dmarc_spy_agency.unspy_all()

    def _dns_query(self, qname, rdtype, *args, **kwargs):
        try:
            return [TXT(1, 16, [self.dmarc_txt_records[qname]])]
        except KeyError:
            raise dns_resolver.NXDOMAIN
Ejemplo n.º 7
0
class DmarcDnsTestsMixin(object):
    """Mixin to help with e-mail tests that need to perform DMARC lookups.

    This mixin makes it easy for unit tests to fake DMARC results, in order
    to prevent the need from looking up real data from real records.

    Unit tests can provide a mapping from domain names to TXT record strings
    in the :py:attr:`dmarc_txt_records` dictionary. When a DMARC lookup is
    performed, this dictionary will be used for the lookup.

    Note that this mixin will also clear the memory cache before each test
    run.

    Attributes:
        dmarc_txt_records (dict):
            A dictionary of domain names to DMARC TXT record strings.
    """
    def setUp(self):
        super(DmarcDnsTestsMixin, self).setUp()

        self.dmarc_txt_records = {}

        self._dmarc_spy_agency = SpyAgency()
        self._dmarc_spy_agency.spy_on(dns_resolver.query,
                                      call_fake=self._dns_query)
        cache.clear()

    def tearDown(self):
        super(DmarcDnsTestsMixin, self).tearDown()

        self._dmarc_spy_agency.unspy_all()

    def _dns_query(self, qname, rdtype, *args, **kwargs):
        try:
            return [TXT(1, 16, [self.dmarc_txt_records[qname]])]
        except KeyError:
            raise dns_resolver.NXDOMAIN
Ejemplo n.º 8
0
class TestViewsPost(TestCase):
    fixtures = ['members.json', 'swots.json', 'users.json']
    auth_data = {
        'user': {
            'userId': 5,
            'email': '*****@*****.**',
            'password': '******'
        }
    }
    spy_agent = SpyAgency()

    def setUp(self):
        setup_token(self, self.auth_data, client)

    def test_add_member_non_existing_user_should_create_invite(self):
        n = len(Invite.objects.all())
        client.post(
            reverse('swot_members:post', kwargs={
                'swot_id': 8,
                'email': '*****@*****.**',
            }),
            content_type='application/json',
            data=json.dumps({})
        )
        actual = len(Invite.objects.all())

        self.assertEqual(actual, n + 1)

    def test_add_member_non_existing_should_send_email(self):
        swot_id, email = 8, '*****@*****.**'
        invitor = '*****@*****.**'
        mock_send_email = self.spy_agent.spy_on(send_invite_email, call_original=False)

        client.post(
            reverse('swot_members:post', kwargs={
                'swot_id': swot_id,
                'email': email,
            }),
            content_type='application/json',
            data=json.dumps({})
        )

        self.assertTrue(mock_send_email.called_with(invitor, email))