Пример #1
0
 def test_contact_get(self):
     # GET on contact form with no pk allows creating new one
     with patch('rapidsms.contrib.registration.views.render') as render:
         request = Mock(method="GET")
         views.contact(request)
     context = render.call_args[0][2]
     # ModelForms create a new unsaved instance
     self.assertIsNotNone(context['contact_form'].instance)
     self.assertTrue(context['contact_form'].instance.is_anonymous)
     self.assertEqual(1, len(context['connection_formset'].forms))
Пример #2
0
 def test_contact_get(self):
     # GET on contact form with no pk allows creating new one
     with patch('rapidsms.contrib.registration.views.render') as render:
         request = Mock(method="GET")
         views.contact(request)
     context = render.call_args[0][2]
     # ModelForms create a new unsaved instance
     self.assertIsNotNone(context['contact_form'].instance)
     self.assertTrue(context['contact_form'].instance.is_anonymous)
     self.assertEqual(1, len(context['connection_formset'].forms))
Пример #3
0
 def test_contact_create(self):
     # POST with no existing contact creates a new one
     name = u'A BRAND NEW CONTACT'
     data = {
         u'name': name,
         u'language': u'wxyz',
         u'submit': u'Save Contact',
         u'connection_set-0-id': u'',
         u'connection_set-0-DELETE': u'',
         u'connection_set-0-backend': u'1',
         u'connection_set-0-contact': u'',
         u'connection_set-0-identity': u'4567',
         u'connection_set-1-id': u'',
         u'connection_set-1-contact': u'',
         u'connection_set-1-identity': u'',
         u'connection_set-1-backend': u'',
         u'connection_set-INITIAL_FORMS': u'0',
         u'connection_set-TOTAL_FORMS': u'2',
         u'connection_set-MAX_NUM_FORMS': u'10',
     }
     with patch('rapidsms.contrib.registration.views.render'):
         request = Mock(method="POST", POST=data)
         request.__class__ = HttpRequest
         self.login()
         request.user = self.user
         retval = views.contact(request)
     self.assertTrue(isinstance(retval, HttpResponseRedirect))
     self.assertEqual(302, retval.status_code)
     Contact.objects.get(name=name)
Пример #4
0
    def test_contact_delete(self):
        # Submitting with Delete button deletes the contact
        contact = self.contacts[0]
        data = {
            u'name': u'The Contact',
            u'language': u'wxyz',
            u'delete_contact': u"dontcare",
            u'connection_set-0-id': u'2',
            u'connection_set-0-DELETE': u'',
            u'connection_set-0-backend': u'1',
            u'connection_set-0-contact': u'1',
            u'connection_set-0-identity': u'4567',
            u'connection_set-1-id': u'',
            u'connection_set-1-contact': u'1',
            u'connection_set-1-identity': u'987654',
            u'connection_set-1-backend': u'1',
            u'connection_set-INITIAL_FORMS': u'1',
            u'connection_set-TOTAL_FORMS': u'2',
            u'connection_set-MAX_NUM_FORMS': u'10',
        }

        with patch('rapidsms.contrib.registration.views.render'):
            request = Mock(method="POST", POST=data)
            request.__class__ = HttpRequest
            self.login()
            request.user = self.user
            retval = views.contact(request, pk=contact.pk)
        self.assertTrue(isinstance(retval, HttpResponseRedirect))
        self.assertEqual(302, retval.status_code)
        self.assertFalse(Contact.objects.filter(pk=contact.pk).exists())
Пример #5
0
 def test_contact_update(self):
     # POST to contact view updates the contact and connections
     contact = self.contacts[0]
     data = {
         u'name': u'The Contact',
         u'language': u'wxyz',
         u'submit': u'Save Contact',
         u'connection_set-0-id': u'2',
         u'connection_set-0-DELETE': u'',
         u'connection_set-0-backend': u'1',
         u'connection_set-0-contact': u'1',
         u'connection_set-0-identity': u'4567',
         u'connection_set-1-id': u'',
         u'connection_set-1-contact': u'1',
         u'connection_set-1-identity': u'',
         u'connection_set-1-backend': u'',
         u'connection_set-INITIAL_FORMS': u'1',
         u'connection_set-TOTAL_FORMS': u'2',
         u'connection_set-MAX_NUM_FORMS': u'10',
     }
     with patch('rapidsms.contrib.registration.views.render'):
         request = Mock(method="POST", POST=data)
         request.__class__ = HttpRequest
         self.login()
         request.user = self.user
         retval = views.contact(request, pk=contact.pk)
     self.assertTrue(isinstance(retval, HttpResponseRedirect))
     self.assertEqual(302, retval.status_code)
     new_contact = Contact.objects.get(pk=contact.pk)
     self.assertEqual(data['name'], new_contact.name)
     self.assertEqual(data['language'], new_contact.language)
     identities = [c.identity for c in contact.connection_set.all()]
     self.assertIn(data['connection_set-0-identity'], identities)
Пример #6
0
    def test_add_connection(self):
        # POST can add a new connection
        contact = self.create_contact()
        # Give it ONE connection
        self.create_connection(data={'contact': contact})

        # Submit form filled out to add another connection

        connections = contact.connection_set.all()
        data = {
            u'name': u'Joe User',
            u'language': u'en',
            u'submit': u"Save Contact",
            u'connection_set-0-id': connections[0].pk,
            u'connection_set-0-identity': connections[0].identity,
            u'connection_set-0-backend': connections[0].backend.pk,
            u'connection_set-0-contact': contact.pk,
            u'connection_set-1-id': u'',
            u'connection_set-1-identity': 'identity',
            u'connection_set-1-backend': connections[0].backend.pk,
            u'connection_set-1-contact': contact.pk,
            u'connection_set-TOTAL_FORMS': u'2',
            u'connection_set-MAX_NUM_FORMS': u'10',
            u'connection_set-INITIAL_FORMS': u'1',
        }
        with patch('rapidsms.contrib.registration.views.render'):
            request = Mock(method="POST", POST=data)
            self.login()
            request.user = self.user
            retval = views.contact(request, pk=contact.pk)
        self.assertTrue(isinstance(retval, HttpResponseRedirect))
        self.assertEqual(302, retval.status_code)
        self.assertEqual(2, Connection.objects.filter(contact=contact).count())
        conn = Connection.objects.get(identity='identity', contact=contact)
        self.assertEqual(connections[0].backend, conn.backend)
Пример #7
0
    def test_add_connection(self):
        # POST can add a new connection
        contact = self.create_contact()
        # Give it ONE connection
        self.create_connection(data={"contact": contact})

        # Submit form filled out to add another connection

        connections = contact.connection_set.all()
        data = {
            u"name": u"Joe User",
            u"language": u"en",
            u"submit": u"Save Contact",
            u"connection_set-0-id": connections[0].pk,
            u"connection_set-0-identity": connections[0].identity,
            u"connection_set-0-backend": connections[0].backend.pk,
            u"connection_set-0-contact": contact.pk,
            u"connection_set-1-id": u"",
            u"connection_set-1-identity": "identity",
            u"connection_set-1-backend": connections[0].backend.pk,
            u"connection_set-1-contact": contact.pk,
            u"connection_set-TOTAL_FORMS": u"2",
            u"connection_set-MAX_NUM_FORMS": u"10",
            u"connection_set-INITIAL_FORMS": u"1",
        }
        with patch("rapidsms.contrib.registration.views.render"):
            request = Mock(method="POST", POST=data)
            self.login()
            request.user = self.user
            retval = views.contact(request, pk=contact.pk)
        self.assertTrue(isinstance(retval, HttpResponseRedirect))
        self.assertEqual(302, retval.status_code)
        self.assertEqual(2, Connection.objects.filter(contact=contact).count())
        conn = Connection.objects.get(identity="identity", contact=contact)
        self.assertEqual(connections[0].backend, conn.backend)
Пример #8
0
 def test_contact_create(self):
     # POST with no existing contact creates a new one
     name = u"A BRAND NEW CONTACT"
     data = {
         u"name": name,
         u"language": u"wxyz",
         u"submit": u"Save Contact",
         u"connection_set-0-id": u"",
         u"connection_set-0-DELETE": u"",
         u"connection_set-0-backend": u"1",
         u"connection_set-0-contact": u"",
         u"connection_set-0-identity": u"4567",
         u"connection_set-1-id": u"",
         u"connection_set-1-contact": u"",
         u"connection_set-1-identity": u"",
         u"connection_set-1-backend": u"",
         u"connection_set-INITIAL_FORMS": u"0",
         u"connection_set-TOTAL_FORMS": u"2",
         u"connection_set-MAX_NUM_FORMS": u"10",
     }
     with patch("rapidsms.contrib.registration.views.render"):
         request = Mock(method="POST", POST=data)
         self.login()
         request.user = self.user
         retval = views.contact(request)
     self.assertTrue(isinstance(retval, HttpResponseRedirect))
     self.assertEqual(302, retval.status_code)
     Contact.objects.get(name=name)
Пример #9
0
    def test_contact_delete(self):
        # Submitting with Delete button deletes the contact
        contact = self.contacts[0]
        data = {
            u"name": u"The Contact",
            u"language": u"wxyz",
            u"submit": u"Delete Contact",
            u"connection_set-0-id": u"2",
            u"connection_set-0-DELETE": u"",
            u"connection_set-0-backend": u"1",
            u"connection_set-0-contact": u"1",
            u"connection_set-0-identity": u"4567",
            u"connection_set-1-id": u"",
            u"connection_set-1-contact": u"1",
            u"connection_set-1-identity": u"987654",
            u"connection_set-1-backend": u"1",
            u"connection_set-INITIAL_FORMS": u"1",
            u"connection_set-TOTAL_FORMS": u"2",
            u"connection_set-MAX_NUM_FORMS": u"10",
        }

        with patch("rapidsms.contrib.registration.views.render"):
            request = Mock(method="POST", POST=data)
            self.login()
            request.user = self.user
            retval = views.contact(request, pk=contact.pk)
        self.assertTrue(isinstance(retval, HttpResponseRedirect))
        self.assertEqual(302, retval.status_code)
        self.assertFalse(Contact.objects.filter(pk=contact.pk).exists())
Пример #10
0
 def test_contact_update(self):
     # POST to contact view updates the contact and connections
     contact = self.contacts[0]
     data = {
         u"name": u"The Contact",
         u"language": u"wxyz",
         u"submit": u"Save Contact",
         u"connection_set-0-id": u"2",
         u"connection_set-0-DELETE": u"",
         u"connection_set-0-backend": u"1",
         u"connection_set-0-contact": u"1",
         u"connection_set-0-identity": u"4567",
         u"connection_set-1-id": u"",
         u"connection_set-1-contact": u"1",
         u"connection_set-1-identity": u"",
         u"connection_set-1-backend": u"",
         u"connection_set-INITIAL_FORMS": u"1",
         u"connection_set-TOTAL_FORMS": u"2",
         u"connection_set-MAX_NUM_FORMS": u"10",
     }
     with patch("rapidsms.contrib.registration.views.render"):
         request = Mock(method="POST", POST=data)
         self.login()
         request.user = self.user
         retval = views.contact(request, pk=contact.pk)
     self.assertTrue(isinstance(retval, HttpResponseRedirect))
     self.assertEqual(302, retval.status_code)
     new_contact = Contact.objects.get(pk=contact.pk)
     self.assertEqual(data["name"], new_contact.name)
     self.assertEqual(data["language"], new_contact.language)
     identities = [c.identity for c in contact.connection_set.all()]
     self.assertIn(data["connection_set-0-identity"], identities)
Пример #11
0
 def test_contact_existing(self):
     # GET on contact form with valid pk renders template with that contact
     contact = self.contacts[0]
     connection = contact.default_connection
     with patch('rapidsms.contrib.registration.views.render') as render:
         request = Mock(method="GET")
         views.contact(request, pk=contact.pk)
     context = render.call_args[0][2]
     self.assertEqual(contact.pk, context['contact'].pk)
     form = context['contact_form']
     data = form.initial
     self.assertEqual(contact.name, data['name'])
     self.assertEqual(contact.pk, form.instance.pk)
     formset = context['connection_formset']
     forms = formset.forms
     instances = [f.instance for f in forms]
     # Connection should be in there
     self.assertIn(connection, instances)
     # Should be 1 more form than we have connections
     self.assertEqual(len(forms), 1 + len(contact.connection_set.all()))
Пример #12
0
 def test_contact_existing(self):
     # GET on contact form with valid pk renders template with that contact
     contact = self.contacts[0]
     connection = contact.default_connection
     with patch('rapidsms.contrib.registration.views.render') as render:
         request = Mock(method="GET")
         views.contact(request, pk=contact.pk)
     context = render.call_args[0][2]
     self.assertEqual(contact.pk, context['contact'].pk)
     form = context['contact_form']
     data = form.initial
     self.assertEqual(contact.name, data['name'])
     self.assertEqual(contact.pk, form.instance.pk)
     formset = context['connection_formset']
     forms = formset.forms
     instances = [f.instance for f in forms]
     # Connection should be in there
     self.assertIn(connection, instances)
     # Should be 1 more form than we have connections
     self.assertEqual(len(forms), 1 + len(contact.connection_set.all()))
Пример #13
0
    def test_delete_connection(self):
        # POST can delete one of the connections
        contact = self.create_contact()
        # Give it two connections
        self.create_connection(data={'contact': contact})
        self.create_connection(data={'contact': contact})

        # Submit form filled out to delete a connection
        connections = contact.connection_set.all()
        data = {
            u'name': u'Joe User',
            u'language': u'en',
            u'submit': u"Save Contact",

            u'connection_set-0-id': connections[0].pk,
            u'connection_set-0-identity': connections[0].identity,
            u'connection_set-0-backend': connections[0].backend.pk,
            u'connection_set-0-contact': contact.pk,

            u'connection_set-1-id': connections[1].pk,
            u'connection_set-1-identity': connections[1].identity,
            u'connection_set-1-backend': connections[1].backend.pk,
            u'connection_set-1-contact': contact.pk,
            u'connection_set-1-DELETE': u"connection_set-1-DELETE",

            u'connection_set-2-id': u'',
            u'connection_set-2-backend': u'',
            u'connection_set-2-identity': u'',
            u'connection_set-2-contact': u'',

            u'connection_set-TOTAL_FORMS': u'3',
            u'connection_set-MAX_NUM_FORMS': u'10',
            u'connection_set-INITIAL_FORMS': u'2',
        }
        old_pk = connections[1].pk
        with patch('rapidsms.contrib.registration.views.render'):
            request = Mock(method="POST", POST=data)
            request.__class__ = HttpRequest
            self.login()
            request.user = self.user
            retval = views.contact(request, pk=contact.pk)
        self.assertTrue(isinstance(retval, HttpResponseRedirect))
        self.assertEqual(302, retval.status_code)
        self.assertFalse(Connection.objects.filter(pk=old_pk).exists())
Пример #14
0
    def test_add_connection(self):
        # POST can add a new connection
        contact = self.create_contact()
        # Give it ONE connection
        self.create_connection(data={'contact': contact})

        # Submit form filled out to add another connection

        connections = contact.connection_set.all()
        data = {
            u'name': u'Joe User',
            u'language': u'en',
            u'submit': u"Save Contact",

            u'connection_set-0-id': connections[0].pk,
            u'connection_set-0-identity': connections[0].identity,
            u'connection_set-0-backend': connections[0].backend.pk,
            u'connection_set-0-contact': contact.pk,

            u'connection_set-1-id': u'',
            u'connection_set-1-identity': 'identity',
            u'connection_set-1-backend': connections[0].backend.pk,
            u'connection_set-1-contact': contact.pk,

            u'connection_set-TOTAL_FORMS': u'2',
            u'connection_set-MAX_NUM_FORMS': u'10',
            u'connection_set-INITIAL_FORMS': u'1',
        }
        with patch('rapidsms.contrib.registration.views.render'):
            request = Mock(method="POST", POST=data)
            request.__class__ = HttpRequest
            self.login()
            request.user = self.user
            retval = views.contact(request, pk=contact.pk)
        self.assertTrue(isinstance(retval, HttpResponseRedirect))
        self.assertEqual(302, retval.status_code)
        self.assertEqual(2, Connection.objects.filter(contact=contact).count())
        conn = Connection.objects.get(identity='identity', contact=contact)
        self.assertEqual(connections[0].backend, conn.backend)
Пример #15
0
    def test_delete_connection(self):
        # POST can delete one of the connections
        contact = self.create_contact()
        # Give it two connections
        self.create_connection(data={"contact": contact})
        self.create_connection(data={"contact": contact})

        # Submit form filled out to delete a connection
        connections = contact.connection_set.all()
        data = {
            u"name": u"Joe User",
            u"language": u"en",
            u"submit": u"Save Contact",
            u"connection_set-0-id": connections[0].pk,
            u"connection_set-0-identity": connections[0].identity,
            u"connection_set-0-backend": connections[0].backend.pk,
            u"connection_set-0-contact": contact.pk,
            u"connection_set-1-id": connections[1].pk,
            u"connection_set-1-identity": connections[1].identity,
            u"connection_set-1-backend": connections[1].backend.pk,
            u"connection_set-1-contact": contact.pk,
            u"connection_set-1-DELETE": u"connection_set-1-DELETE",
            u"connection_set-2-id": u"",
            u"connection_set-2-backend": u"",
            u"connection_set-2-identity": u"",
            u"connection_set-2-contact": u"",
            u"connection_set-TOTAL_FORMS": u"3",
            u"connection_set-MAX_NUM_FORMS": u"10",
            u"connection_set-INITIAL_FORMS": u"2",
        }
        old_pk = connections[1].pk
        with patch("rapidsms.contrib.registration.views.render"):
            request = Mock(method="POST", POST=data)
            self.login()
            request.user = self.user
            retval = views.contact(request, pk=contact.pk)
        self.assertTrue(isinstance(retval, HttpResponseRedirect))
        self.assertEqual(302, retval.status_code)
        self.assertFalse(Connection.objects.filter(pk=old_pk).exists())
Пример #16
0
 def test_contact_existing_404(self):
     # Trying to edit a non-existing contact raises a 404
     with self.assertRaises(Http404):
         views.contact(Mock(), pk=27)