예제 #1
0
    def test_update_and_publish_opportunity_as_admin(self):
        data = {
            'department': str(self.department1.id), 'contact_email': self.staff.email,
            'title': 'test_create_edit_publish', 'description': 'bar',
            'planned_publish': datetime.date.today(),
            'planned_submission_start': datetime.date.today(),
            'planned_submission_end': datetime.date.today() + datetime.timedelta(days=5),
            'save_type': 'save', 'subcategories-{}'.format(Category.query.all()[-1].id): 'on',
            'opportunity_type': self.opportunity_type.id
        }

        self.login_user(self.admin)
        self.assertEquals(Opportunity.query.count(), 4)

        with mail.record_messages() as outbox:
            self.client.post('/beacon/admin/opportunities/new', data=data)

            self.assertEquals(Opportunity.query.count(), 5)
            # doesn't send the opportunity yet
            self.assertEquals(len(outbox), 0)

            data.update({'save_type': 'publish'})
            self.client.post('/beacon/admin/opportunities/{}'.format(
                Opportunity.query.filter(Opportunity.title == 'test_create_edit_publish').first().id),
                data=data
            )
            # sends the opportunity when updated with the proper save type
            self.assertEquals(len(outbox), 1)
            self.assertEquals(outbox[0].subject, '[Pittsburgh Purchasing] A new City of Pittsburgh opportunity from Beacon!')
예제 #2
0
    def test_signup_for_opportunity(self):
        self.opportunity1.is_public = True
        self.opportunity1.planned_publish = datetime.date.today(
        ) - datetime.timedelta(2)
        db.session.commit()

        with mail.record_messages() as outbox:
            self.assertEquals(Vendor.query.count(), 1)
            post = self.client.post('/opportunities/{}'.format(
                self.opportunity1.id),
                                    data={
                                        'email': '*****@*****.**',
                                        'business_name': 'foo'
                                    })
            # should create a new vendor
            self.assertEquals(Vendor.query.count(), 2)

            # should subscribe that vendor to the opportunity
            self.assertEquals(
                len(
                    Vendor.query.filter(
                        Vendor.email == '*****@*****.**').first().opportunities),
                1)
            self.assertTrue(self.opportunity1.id in [
                i.id for i in Vendor.query.filter(
                    Vendor.email == '*****@*****.**').first().opportunities
            ])

            # should redirect and flash properly
            self.assertEquals(post.status_code, 302)
            self.assert_flashes('Successfully subscribed for updates!',
                                'alert-success')

            self.assertEquals(len(outbox), 1)
예제 #3
0
파일: test_front.py 프로젝트: dobtco/beacon
    def test_signup_for_opportunity(self):
        self.opportunity1.is_public = True
        self.opportunity1.planned_publish = datetime.date.today() - datetime.timedelta(2)
        db.session.commit()

        with mail.record_messages() as outbox:
            self.assertEquals(Vendor.query.count(), 1)
            post = self.client.post(
                "/opportunities/{}?form=signup_form".format(self.opportunity1.id),
                data={"email": "*****@*****.**", "business_name": "foo"},
            )
            # should create a new vendor
            self.assertEquals(Vendor.query.count(), 2)

            # should subscribe that vendor to the opportunity
            self.assertEquals(len(Vendor.query.filter(Vendor.email == "*****@*****.**").first().opportunities), 1)
            self.assertTrue(
                self.opportunity1.id
                in [i.id for i in Vendor.query.filter(Vendor.email == "*****@*****.**").first().opportunities]
            )

            # should redirect and flash properly
            self.assertEquals(post.status_code, 302)
            self.assert_flashes(
                Markup(
                    "Successfully subscribed for updates! <a href="
                    + url_for("front.signup")
                    + ">Sign up</a> for alerts about future opportunities."
                ),
                "alert-success",
            )

            self.assertEquals(len(outbox), 1)
예제 #4
0
    def test_signup_for_opportunity(self):
        self.opportunity1.is_public = True
        self.opportunity1.planned_publish = datetime.date.today() - datetime.timedelta(2)
        db.session.commit()

        with mail.record_messages() as outbox:
            self.assertEquals(Vendor.query.count(), 1)
            post = self.client.post('/opportunities/{}'.format(self.opportunity1.id), data={
                'email': '*****@*****.**', 'business_name': 'foo'
            })
            # should create a new vendor
            self.assertEquals(Vendor.query.count(), 2)

            # should subscribe that vendor to the opportunity
            self.assertEquals(len(Vendor.query.filter(Vendor.email == '*****@*****.**').first().opportunities), 1)
            self.assertTrue(
                self.opportunity1.id in [
                    i.id for i in Vendor.query.filter(Vendor.email == '*****@*****.**').first().opportunities
                ]
            )

            # should redirect and flash properly
            self.assertEquals(post.status_code, 302)
            self.assert_flashes('Successfully subscribed for updates!', 'alert-success')

            self.assertEquals(len(outbox), 1)
예제 #5
0
 def test_vendor_signup_unpublished(self):
     with mail.record_messages() as outbox:
         # vendor should not be able to sign up for unpublished opp
         bad_contract = self.client.post('/opportunities', data={
             'email': '*****@*****.**', 'business_name': 'foo',
             'opportunity': str(self.opportunity3.id),
         })
         self.assertEquals(len(Vendor.query.get(1).opportunities), 0)
         self.assertTrue('not a valid choice.' in bad_contract.data)
         self.assertEquals(len(outbox), 0)
예제 #6
0
파일: test_front.py 프로젝트: dobtco/beacon
 def test_ask_a_question(self):
     with mail.record_messages() as outbox:
         self.client.post(
             self.opportunity_url + "?form=question_form",
             data=dict(
                 question="look at my question", email=self.vendor.email, business_name=self.vendor.business_name
             ),
         )
         self.assertEquals(Question.query.count(), 1)
         self.assertEquals(len(outbox), 2)
         self.assertTrue("look at my question" in outbox[0].body)
예제 #7
0
    def test_pending_notification_email(self):
        self.login_user(self.admin)
        self.assertFalse(self.opportunity3.publish_notification_sent)

        with mail.record_messages() as outbox:
            self.client.get('/beacon/admin/opportunities/{}/publish'.format(
                self.opportunity3.id))
            self.assertTrue(self.opportunity3.is_published)
            self.assertTrue(self.opportunity3.is_public)
            self.assertTrue(self.opportunity3.publish_notification_sent)
            self.assertEquals(len(outbox), 2)
예제 #8
0
    def test_pending_notification_email(self):
        self.login_user(self.admin)
        self.assertFalse(self.opportunity3.publish_notification_sent)

        with mail.record_messages() as outbox:
            self.client.get('/beacon/admin/opportunities/{}/publish'.format(
                self.opportunity3.id
            ))
            self.assertTrue(self.opportunity3.is_published)
            self.assertTrue(self.opportunity3.is_public)
            self.assertTrue(self.opportunity3.publish_notification_sent)
            self.assertEquals(len(outbox), 2)
예제 #9
0
파일: test_front.py 프로젝트: dobtco/beacon
 def test_same_person_emailed_once(self):
     self.opportunity3.created_by = self.admin
     self.opportunity3.save()
     with mail.record_messages() as outbox:
         self.client.post(
             self.opportunity_url + "?form=question_form",
             data=dict(
                 question="look at my question", email=self.vendor.email, business_name=self.vendor.business_name
             ),
         )
         self.assertEquals(Question.query.count(), 1)
         self.assertEquals(len(outbox), 1)
예제 #10
0
    def test_beacon_new_opportunity_nightly(self):
        opportunity_id = self.opportunity.id
        nightly = BeaconNewOppotunityOpenJob(time_override=True)
        scheduled, existing_job = nightly.schedule_job()
        self.assertFalse(existing_job)

        with mail.record_messages() as outbox:
            nightly.run_job(scheduled)
            self.assertEquals(len(outbox), 2)
            self.assertEquals(
                outbox[0].subject,
                '[Pittsburgh Purchasing] A new City of Pittsburgh opportunity from Beacon!'
            )
            self.assertTrue(Opportunity.query.get(opportunity_id).publish_notification_sent)
예제 #11
0
 def test_pending_opportunity_admin(self):
     self.login_user(self.admin)
     self.assertFalse(Opportunity.query.get(self.opportunity3.id).is_public)
     admin_pending = self.client.get('/beacon/admin/opportunities/pending')
     self.assert200(admin_pending)
     self.assertEquals(len(self.get_context_variable('pending')), 1)
     self.assertTrue('Publish' in admin_pending.data)
     with mail.record_messages() as outbox:
         admin_publish = self.client.get('/beacon/admin/opportunities/{}/publish'.format(
             self.opportunity3.id
         ))
         self.assert_flashes('Opportunity successfully published!', 'alert-success')
         self.assertEquals(admin_publish.status_code, 302)
         self.assertTrue(self.opportunity3.is_public)
         self.assertTrue(self.opportunity3.published_at is not None)
예제 #12
0
 def test_vendor_signup_unpublished(self):
     with mail.record_messages() as outbox:
         # vendor should not be able to sign up for unpublished opp
         bad_contract = self.client.post('/opportunities',
                                         data={
                                             'email':
                                             '*****@*****.**',
                                             'business_name':
                                             'foo',
                                             'opportunity':
                                             str(self.opportunity3.id),
                                         })
         self.assertEquals(len(Vendor.query.get(1).opportunities), 0)
         self.assertTrue('not a valid choice.' in bad_contract.data)
         self.assertEquals(len(outbox), 0)
예제 #13
0
 def test_pending_opportunity_admin(self):
     self.login_user(self.admin)
     self.assertFalse(Opportunity.query.get(self.opportunity3.id).is_public)
     admin_pending = self.client.get('/beacon/admin/opportunities/pending')
     self.assert200(admin_pending)
     self.assertEquals(len(self.get_context_variable('pending')), 1)
     self.assertTrue('Publish' in admin_pending.data)
     with mail.record_messages() as outbox:
         admin_publish = self.client.get(
             '/beacon/admin/opportunities/{}/publish'.format(
                 self.opportunity3.id))
         self.assert_flashes('Opportunity successfully published!',
                             'alert-success')
         self.assertEquals(admin_publish.status_code, 302)
         self.assertTrue(self.opportunity3.is_public)
         self.assertTrue(self.opportunity3.published_at is not None)
예제 #14
0
    def test_pending_notification_email_gated(self):
        self.login_user(self.admin)
        self.opportunity3.planned_publish = datetime.date.today(
        ) + datetime.timedelta(1)
        self.assertFalse(self.opportunity3.publish_notification_sent)
        db.session.commit()

        with mail.record_messages() as outbox:
            self.client.get('/beacon/admin/opportunities/{}/publish'.format(
                self.opportunity3.id))
            self.assertFalse(self.opportunity3.is_published)
            self.assertTrue(self.opportunity3.is_public)
            self.assertFalse(self.opportunity3.publish_notification_sent)
            self.assertEquals(len(outbox), 1)
            self.assertTrue('A new City of Pittsburgh opportunity from Beacon'
                            not in [i.subject for i in outbox])
예제 #15
0
    def test_update_and_publish_opportunity_as_admin(self):
        data = {
            'department':
            str(self.department1.id),
            'contact_email':
            self.staff.email,
            'title':
            'test_create_edit_publish',
            'description':
            'bar',
            'planned_publish':
            datetime.date.today(),
            'planned_submission_start':
            datetime.date.today(),
            'planned_submission_end':
            datetime.date.today() + datetime.timedelta(days=5),
            'save_type':
            'save',
            'subcategories-{}'.format(Category.query.all()[-1].id):
            'on',
            'opportunity_type':
            self.opportunity_type.id
        }

        self.login_user(self.admin)
        self.assertEquals(Opportunity.query.count(), 4)

        with mail.record_messages() as outbox:
            self.client.post('/beacon/admin/opportunities/new', data=data)

            self.assertEquals(Opportunity.query.count(), 5)
            # doesn't send the opportunity yet
            self.assertEquals(len(outbox), 0)

            data.update({'save_type': 'publish'})
            self.client.post('/beacon/admin/opportunities/{}'.format(
                Opportunity.query.filter(
                    Opportunity.title ==
                    'test_create_edit_publish').first().id),
                             data=data)
            # sends the opportunity when updated with the proper save type
            self.assertEquals(len(outbox), 1)
            self.assertEquals(
                outbox[0].subject,
                '[Pittsburgh Purchasing] A new City of Pittsburgh opportunity from Beacon!'
            )
예제 #16
0
    def test_pending_notification_email_gated(self):
        self.login_user(self.admin)
        self.opportunity3.planned_publish = datetime.date.today() + datetime.timedelta(1)
        self.assertFalse(self.opportunity3.publish_notification_sent)
        db.session.commit()

        with mail.record_messages() as outbox:
            self.client.get('/beacon/admin/opportunities/{}/publish'.format(
                self.opportunity3.id
            ))
            self.assertFalse(self.opportunity3.is_published)
            self.assertTrue(self.opportunity3.is_public)
            self.assertFalse(self.opportunity3.publish_notification_sent)
            self.assertEquals(len(outbox), 1)
            self.assertTrue(
                'A new City of Pittsburgh opportunity from Beacon' not in
                [i.subject for i in outbox]
            )
예제 #17
0
파일: test_admin.py 프로젝트: dobtco/beacon
    def test_edit_questions(self):
        self.q1.answer_text = 'an answer'
        self.q1.save()
        self.login_user(self.admin)
        questions = self.client.get(self.question_url)
        self.assertTrue('Edited on' not in questions.data)
        self.assertTrue('No unanswered questions' in questions.data)

        with mail.record_messages() as outbox:
            self.client.post(
                self.answer_url, data=dict(answer_text='new answer')
            )
            self.assertEquals(len(outbox), 0)

        new_questions = self.client.get(self.question_url)
        self.assertEquals(len(self.get_context_variable('answered')), 2)
        self.assertTrue('Edited on' in new_questions.data)
        self.assertTrue('No unanswered questions' in new_questions.data)
예제 #18
0
    def test_create_and_publish_opportunity_as_admin(self):
        self.login_user(self.admin)
        self.assertEquals(Opportunity.query.count(), 4)

        with mail.record_messages() as outbox:
            self.client.post('/beacon/admin/opportunities/new', data={
                'department': str(self.department1.id), 'contact_email': self.staff.email,
                'title': 'foo', 'description': 'bar',
                'planned_publish': datetime.date.today(),
                'planned_submission_start': datetime.date.today(),
                'planned_submission_end': datetime.date.today() + datetime.timedelta(days=5),
                'save_type': 'publish', 'subcategories-{}'.format(Category.query.all()[-1].id): 'on',
                'opportunity_type': self.opportunity_type.id
            })

            self.assertEquals(Opportunity.query.count(), 5)
            # should send to the single vendor signed up to receive info
            # about that category
            self.assertEquals(len(outbox), 1)
            self.assertEquals(outbox[0].subject, '[Pittsburgh Purchasing] A new City of Pittsburgh opportunity from Beacon!')
예제 #19
0
파일: test_admin.py 프로젝트: dobtco/beacon
    def test_answer_questions(self):
        self.login_user(self.admin)
        self.assert200(self.client.get(self.answer_url))
        with mail.record_messages() as outbox:
            self.client.post(
                self.answer_url, data=dict(
                    answer_text='ANSWER TO YOUR QUESTION'
                )
            )
            self.client.get(self.question_url)
            self.assertEquals(len(self.get_context_variable('answered')), 2)
            self.assertEquals(Question.query.get(self.q1.id).answered_by, self.admin)

            # should send 2 mails -- 1 to the other opportunity person, 1 to vendor
            self.assertEquals(len(outbox), 2)
            sent_to = []
            for i in outbox:
                sent_to.extend(list(i.send_to))
            self.assertIn('*****@*****.**', sent_to)
            self.assertNotIn(self.admin.email, sent_to)
            self.assertTrue('ANSWER TO YOUR QUESTION' in outbox[0].body)
            self.assertTrue('New answer to a questio on Beacon' in outbox[0].subject)
예제 #20
0
    def test_create_and_publish_opportunity_as_admin(self):
        self.login_user(self.admin)
        self.assertEquals(Opportunity.query.count(), 4)

        with mail.record_messages() as outbox:
            self.client.post(
                '/beacon/admin/opportunities/new',
                data={
                    'department':
                    str(self.department1.id),
                    'contact_email':
                    self.staff.email,
                    'title':
                    'foo',
                    'description':
                    'bar',
                    'planned_publish':
                    datetime.date.today(),
                    'planned_submission_start':
                    datetime.date.today(),
                    'planned_submission_end':
                    datetime.date.today() + datetime.timedelta(days=5),
                    'save_type':
                    'publish',
                    'subcategories-{}'.format(Category.query.all()[-1].id):
                    'on',
                    'opportunity_type':
                    self.opportunity_type.id
                })

            self.assertEquals(Opportunity.query.count(), 5)
            # should send to the single vendor signed up to receive info
            # about that category
            self.assertEquals(len(outbox), 1)
            self.assertEquals(
                outbox[0].subject,
                '[Pittsburgh Purchasing] A new City of Pittsburgh opportunity from Beacon!'
            )
예제 #21
0
파일: test_front.py 프로젝트: dobtco/beacon
    def test_signup(self):
        admin_role = RoleFactory.create(name="admin")
        superadmin_role = RoleFactory.create(name="superadmin")

        UserFactory.create(roles=[admin_role])
        UserFactory.create(email="*****@*****.**", roles=[superadmin_role])

        response = self.client.get("/signup")
        self.assert200(response)
        subcats = json.loads(self.get_context_variable("subcategories"))

        # assert three categories (plus the total category)
        self.assertEquals(len(subcats.keys()), 4)
        # assert five total subcatgories (plus 5 in the total field)
        self.assertEquals(len([item for sublist in subcats.values() for item in sublist]), 10)

        # assert email, business, categories needed
        no_email_post = self.client.post("/signup", data=dict(first_name="foo"))

        self.assert200(no_email_post)
        self.assertTrue(no_email_post.data.count("alert-danger"), 3)
        # ensure that there are two required field notes
        self.assertTrue(no_email_post.data.count("This field is required"), 2)

        # assert valid email address
        invalid_email_post = self.client.post("/signup", data=dict(email="INVALID", business_name="test"))

        self.assert200(invalid_email_post)
        self.assertTrue(invalid_email_post.data.count("alert-danger"), 1)
        self.assertTrue(invalid_email_post.data.count("Invalid email address."), 1)

        # assert you need at least one category
        invalid_no_categories = self.client.post("/signup", data=dict(email="*****@*****.**", business_name="foo"))
        self.assert200(invalid_no_categories)
        self.assertTrue(invalid_no_categories.data.count("alert-danger"), 1)
        self.assertTrue(invalid_no_categories.data.count("You must select at least one!"), 1)

        # assert valid categories

        with mail.record_messages() as outbox:

            # successful post with only one set of subcategories
            success_post = self.client.post(
                "/signup",
                data={
                    "email": "*****@*****.**",
                    "business_name": "foo",
                    "subcategories-1": "on",
                    "categories": "Apparel",
                    "subscribed_to_newsletter": True,
                },
            )

            with self.client.session_transaction() as session:
                assert "email" in session
                assert "business_name" in session
                self.assertEquals(session["email"], "*****@*****.**")
                self.assertEquals(session["business_name"], "foo")

            self.assertEquals(success_post.status_code, 302)
            self.assertEquals(success_post.location, "http://localhost/")
            # should send three emails
            # one to the vendor, one to the admins
            self.assertEquals(len(outbox), 2)
            self.assertEquals(Vendor.query.count(), 1)
            self.assertTrue(Vendor.query.first().subscribed_to_newsletter)
            self.assertEquals(len(Vendor.query.first().categories), 1)
            self.assert_flashes("Thank you for signing up! Check your email for more information", "alert-success")

            # successful post with two sets of subcategories
            success_post_everything = self.client.post(
                "/signup",
                data={
                    "email": "*****@*****.**",
                    "business_name": "foo",
                    "subcategories-1": "on",
                    "subcategories-2": "on",
                    "subcategories-3": "on",
                    "subcategories-4": "on",
                    "subcategories-5": "on",
                    "categories": "Apparel",
                    "subscribed_to_newsletter": True,
                },
            )

            self.assertEquals(success_post_everything.status_code, 302)
            self.assertEquals(success_post_everything.location, "http://localhost/")
            self.assertEquals(len(outbox), 4)
            self.assertEquals(Vendor.query.count(), 2)
            self.assertEquals(len(Vendor.query.filter(Vendor.email == "*****@*****.**").first().categories), 5)
            self.assert_flashes("Thank you for signing up! Check your email for more information", "alert-success")

            # successful post with existing email should update the profile, not send message
            success_post_old_email = self.client.post(
                "/signup",
                data={
                    "email": "*****@*****.**",
                    "business_name": "foo",
                    "subcategories-1": "on",
                    "subcategories-2": "on",
                    "subcategories-3": "on",
                    "categories": "Apparel",
                    "subscribed_to_newsletter": True,
                },
            )

            self.assertEquals(success_post_old_email.status_code, 302)
            self.assertEquals(success_post_old_email.location, "http://localhost/")
            self.assertEquals(len(outbox), 4)
            self.assertEquals(Vendor.query.count(), 2)
            self.assertEquals(len(Vendor.query.filter(Vendor.email == "*****@*****.**").first().categories), 5)
            self.assert_flashes(
                "You are already signed up! Your profile was updated with this new information", "alert-info"
            )

            admin_mail, vendor_mail = 0, 0
            for _mail in outbox:
                if "new vendor has signed up on beacon" in _mail.subject:
                    admin_mail += 1
                if "Thank you for signing up" in _mail.subject:
                    vendor_mail += 1

            self.assertEquals(admin_mail, 2)
            self.assertEquals(vendor_mail, 2)

            with self.client.session_transaction() as session:
                assert "email" in session
                assert "business_name" in session
                self.assertEquals(session["email"], "*****@*****.**")
                self.assertEquals(session["business_name"], "foo")
예제 #22
0
    def test_signup(self):
        admin_role = RoleFactory.create(name='admin')
        superadmin_role = RoleFactory.create(name='superadmin')

        UserFactory.create(role=admin_role)
        UserFactory.create(email='*****@*****.**', role=superadmin_role)

        response = self.client.get('/signup')
        self.assert200(response)
        subcats = json.loads(self.get_context_variable('subcategories'))

        # assert three categories (plus the total category)
        self.assertEquals(len(subcats.keys()), 4)
        # assert five total subcatgories (plus 5 in the total field)
        self.assertEquals(
            len([item for sublist in subcats.values() for item in sublist]),
            10)

        # assert email, business, categories needed
        no_email_post = self.client.post('/signup',
                                         data=dict(first_name='foo'))

        self.assert200(no_email_post)
        self.assertTrue(no_email_post.data.count('alert-danger'), 3)
        # ensure that there are two required field notes
        self.assertTrue(no_email_post.data.count('This field is required'), 2)

        # assert valid email address
        invalid_email_post = self.client.post('/signup',
                                              data=dict(email='INVALID',
                                                        business_name='test'))

        self.assert200(invalid_email_post)
        self.assertTrue(invalid_email_post.data.count('alert-danger'), 1)
        self.assertTrue(
            invalid_email_post.data.count('Invalid email address.'), 1)

        # assert you need at least one category
        invalid_no_categories = self.client.post('/signup',
                                                 data=dict(
                                                     email='*****@*****.**',
                                                     business_name='foo'))
        self.assert200(invalid_no_categories)
        self.assertTrue(invalid_no_categories.data.count('alert-danger'), 1)
        self.assertTrue(
            invalid_no_categories.data.count('You must select at least one!'),
            1)

        # assert valid categories

        with mail.record_messages() as outbox:

            # successful post with only one set of subcategories
            success_post = self.client.post('/signup',
                                            data={
                                                'email': '*****@*****.**',
                                                'business_name': 'foo',
                                                'subcategories-1': 'on',
                                                'categories': 'Apparel',
                                                'subscribed_to_newsletter':
                                                True
                                            })

            with self.client.session_transaction() as session:
                assert 'email' in session
                assert 'business_name' in session
                self.assertEquals(session['email'], '*****@*****.**')
                self.assertEquals(session['business_name'], 'foo')

            self.assertEquals(success_post.status_code, 302)
            self.assertEquals(success_post.location, 'http://localhost/')
            # should send three emails
            # one to the vendor, one to the admins
            self.assertEquals(len(outbox), 2)
            self.assertEquals(Vendor.query.count(), 1)
            self.assertTrue(Vendor.query.first().subscribed_to_newsletter)
            self.assertEquals(len(Vendor.query.first().categories), 1)
            self.assert_flashes(
                'Thank you for signing up! Check your email for more information',
                'alert-success')

            # successful post with two sets of subcategories
            success_post_everything = self.client.post(
                '/signup',
                data={
                    'email': '*****@*****.**',
                    'business_name': 'foo',
                    'subcategories-1': 'on',
                    'subcategories-2': 'on',
                    'subcategories-3': 'on',
                    'subcategories-4': 'on',
                    'subcategories-5': 'on',
                    'categories': 'Apparel',
                    'subscribed_to_newsletter': True
                })

            self.assertEquals(success_post_everything.status_code, 302)
            self.assertEquals(success_post_everything.location,
                              'http://localhost/')
            self.assertEquals(len(outbox), 4)
            self.assertEquals(Vendor.query.count(), 2)
            self.assertEquals(
                len(
                    Vendor.query.filter(
                        Vendor.email == '*****@*****.**').first().categories), 5)
            self.assert_flashes(
                'Thank you for signing up! Check your email for more information',
                'alert-success')

            # successful post with existing email should update the profile, not send message
            success_post_old_email = self.client.post(
                '/signup',
                data={
                    'email': '*****@*****.**',
                    'business_name': 'foo',
                    'subcategories-1': 'on',
                    'subcategories-2': 'on',
                    'subcategories-3': 'on',
                    'categories': 'Apparel',
                    'subscribed_to_newsletter': True
                })

            self.assertEquals(success_post_old_email.status_code, 302)
            self.assertEquals(success_post_old_email.location,
                              'http://localhost/')
            self.assertEquals(len(outbox), 4)
            self.assertEquals(Vendor.query.count(), 2)
            self.assertEquals(
                len(
                    Vendor.query.filter(
                        Vendor.email == '*****@*****.**').first().categories), 5)
            self.assert_flashes(
                "You are already signed up! Your profile was updated with this new information",
                'alert-info')

            admin_mail, vendor_mail = 0, 0
            for _mail in outbox:
                if 'new vendor has signed up on beacon' in _mail.subject:
                    admin_mail += 1
                if 'Thank you for signing up' in _mail.subject:
                    vendor_mail += 1

            self.assertEquals(admin_mail, 2)
            self.assertEquals(vendor_mail, 2)

            with self.client.session_transaction() as session:
                assert 'email' in session
                assert 'business_name' in session
                self.assertEquals(session['email'], '*****@*****.**')
                self.assertEquals(session['business_name'], 'foo')
예제 #23
0
    def test_signup(self):
        admin_role = RoleFactory.create(name='admin')
        superadmin_role = RoleFactory.create(name='superadmin')

        UserFactory.create(role=admin_role)
        UserFactory.create(email='*****@*****.**', role=superadmin_role)

        response = self.client.get('/signup')
        self.assert200(response)
        subcats = json.loads(self.get_context_variable('subcategories'))

        # assert three categories (plus the total category)
        self.assertEquals(len(subcats.keys()), 4)
        # assert five total subcatgories (plus 5 in the total field)
        self.assertEquals(len([item for sublist in subcats.values() for item in sublist]), 10)

        # assert email, business, categories needed
        no_email_post = self.client.post('/signup', data=dict(
            first_name='foo'
        ))

        self.assert200(no_email_post)
        self.assertTrue(no_email_post.data.count('alert-danger'), 3)
        # ensure that there are two required field notes
        self.assertTrue(no_email_post.data.count('This field is required'), 2)

        # assert valid email address
        invalid_email_post = self.client.post('/signup', data=dict(
            email='INVALID',
            business_name='test'
        ))

        self.assert200(invalid_email_post)
        self.assertTrue(invalid_email_post.data.count('alert-danger'), 1)
        self.assertTrue(invalid_email_post.data.count('Invalid email address.'), 1)

        # assert you need at least one category
        invalid_no_categories = self.client.post('/signup', data=dict(
            email='*****@*****.**',
            business_name='foo'
        ))
        self.assert200(invalid_no_categories)
        self.assertTrue(invalid_no_categories.data.count('alert-danger'), 1)
        self.assertTrue(invalid_no_categories.data.count('You must select at least one!'), 1)

        # assert valid categories

        with mail.record_messages() as outbox:

            # successful post with only one set of subcategories
            success_post = self.client.post('/signup', data={
                'email': '*****@*****.**',
                'business_name': 'foo',
                'subcategories-1': 'on',
                'categories': 'Apparel',
                'subscribed_to_newsletter': True
            })

            with self.client.session_transaction() as session:
                assert 'email' in session
                assert 'business_name' in session
                self.assertEquals(session['email'], '*****@*****.**')
                self.assertEquals(session['business_name'], 'foo')

            self.assertEquals(success_post.status_code, 302)
            self.assertEquals(success_post.location, 'http://localhost/')
            # should send three emails
            # one to the vendor, one to the admins
            self.assertEquals(len(outbox), 2)
            self.assertEquals(Vendor.query.count(), 1)
            self.assertTrue(Vendor.query.first().subscribed_to_newsletter)
            self.assertEquals(len(Vendor.query.first().categories), 1)
            self.assert_flashes(
                'Thank you for signing up! Check your email for more information', 'alert-success'
            )

            # successful post with two sets of subcategories
            success_post_everything = self.client.post('/signup', data={
                'email': '*****@*****.**',
                'business_name': 'foo',
                'subcategories-1': 'on',
                'subcategories-2': 'on',
                'subcategories-3': 'on',
                'subcategories-4': 'on',
                'subcategories-5': 'on',
                'categories': 'Apparel',
                'subscribed_to_newsletter': True
            })

            self.assertEquals(success_post_everything.status_code, 302)
            self.assertEquals(success_post_everything.location, 'http://localhost/')
            self.assertEquals(len(outbox), 4)
            self.assertEquals(Vendor.query.count(), 2)
            self.assertEquals(len(Vendor.query.filter(Vendor.email == '*****@*****.**').first().categories), 5)
            self.assert_flashes('Thank you for signing up! Check your email for more information', 'alert-success')

            # successful post with existing email should update the profile, not send message
            success_post_old_email = self.client.post('/signup', data={
                'email': '*****@*****.**',
                'business_name': 'foo',
                'subcategories-1': 'on',
                'subcategories-2': 'on',
                'subcategories-3': 'on',
                'categories': 'Apparel',
                'subscribed_to_newsletter': True
            })

            self.assertEquals(success_post_old_email.status_code, 302)
            self.assertEquals(success_post_old_email.location, 'http://localhost/')
            self.assertEquals(len(outbox), 4)
            self.assertEquals(Vendor.query.count(), 2)
            self.assertEquals(len(Vendor.query.filter(Vendor.email == '*****@*****.**').first().categories), 5)
            self.assert_flashes(
                "You are already signed up! Your profile was updated with this new information", 'alert-info'
            )

            admin_mail, vendor_mail = 0, 0
            for _mail in outbox:
                if 'new vendor has signed up on beacon' in _mail.subject:
                    admin_mail += 1
                if 'Thank you for signing up' in _mail.subject:
                    vendor_mail += 1

            self.assertEquals(admin_mail, 2)
            self.assertEquals(vendor_mail, 2)

            with self.client.session_transaction() as session:
                assert 'email' in session
                assert 'business_name' in session
                self.assertEquals(session['email'], '*****@*****.**')
                self.assertEquals(session['business_name'], 'foo')