def setUp(self):
        super(TestProposalAnalytics, self).setUp()
        self.proposal = ProposalFactory()
        self.user1 = str(uuid.uuid4())
        self.user2 = str(uuid.uuid4())
        self.shared1 = SharedProposalFactory(proposal_id=self.proposal.id)
        self.shared2 = SharedProposalFactory(proposal_id=self.proposal.id)

        self.start = datetime.utcnow()

        def get_future(seconds):
            return self.start + timedelta(seconds=seconds)

        EventFactory(shared_proposal=self.shared1, kind="load", user_uid=self.user1, created_at=self.start)
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user1, created_at=get_future(15), data={})
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user1, created_at=get_future(30), data={})

        EventFactory(shared_proposal=self.shared1, kind="load", user_uid=self.user1, created_at=get_future(500))
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user1, created_at=get_future(555), data={})

        EventFactory(shared_proposal=self.shared1, kind="load", user_uid=self.user2, created_at=get_future(10))
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user2, created_at=get_future(50), data={})

        EventFactory(shared_proposal=self.shared1, kind="outbound_click", user_uid=self.user2, data={"url": "proppy.io"})
        EventFactory(shared_proposal=self.shared1, kind="outbound_click", user_uid=self.user1, data={"url": "google.io"})
        EventFactory(shared_proposal=self.shared1, kind="outbound_click", user_uid=self.user1, data={"url": "proppy.io"})

        EventFactory(shared_proposal=self.shared2, kind="load", user_uid=self.user1, created_at=self.start)
        EventFactory(shared_proposal=self.shared2, kind="load", user_uid=self.user2, created_at=get_future(10))
        EventFactory(shared_proposal=self.shared2, kind="ping", user_uid=self.user2, created_at=get_future(25), data={})
        EventFactory(shared_proposal=self.shared2, kind="outbound_click", user_uid=self.user1, data={"url": "google.io"})
class TestProposalAnalytics(DatabaseTest):
    def setUp(self):
        super(TestProposalAnalytics, self).setUp()
        self.proposal = ProposalFactory()
        self.user1 = str(uuid.uuid4())
        self.user2 = str(uuid.uuid4())
        self.shared1 = SharedProposalFactory(proposal_id=self.proposal.id)
        self.shared2 = SharedProposalFactory(proposal_id=self.proposal.id)

        self.start = datetime.utcnow()

        def get_future(seconds):
            return self.start + timedelta(seconds=seconds)

        EventFactory(shared_proposal=self.shared1, kind="load", user_uid=self.user1, created_at=self.start)
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user1, created_at=get_future(15), data={})
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user1, created_at=get_future(30), data={})

        EventFactory(shared_proposal=self.shared1, kind="load", user_uid=self.user1, created_at=get_future(500))
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user1, created_at=get_future(555), data={})

        EventFactory(shared_proposal=self.shared1, kind="load", user_uid=self.user2, created_at=get_future(10))
        EventFactory(shared_proposal=self.shared1, kind="ping", user_uid=self.user2, created_at=get_future(50), data={})

        EventFactory(shared_proposal=self.shared1, kind="outbound_click", user_uid=self.user2, data={"url": "proppy.io"})
        EventFactory(shared_proposal=self.shared1, kind="outbound_click", user_uid=self.user1, data={"url": "google.io"})
        EventFactory(shared_proposal=self.shared1, kind="outbound_click", user_uid=self.user1, data={"url": "proppy.io"})

        EventFactory(shared_proposal=self.shared2, kind="load", user_uid=self.user1, created_at=self.start)
        EventFactory(shared_proposal=self.shared2, kind="load", user_uid=self.user2, created_at=get_future(10))
        EventFactory(shared_proposal=self.shared2, kind="ping", user_uid=self.user2, created_at=get_future(25), data={})
        EventFactory(shared_proposal=self.shared2, kind="outbound_click", user_uid=self.user1, data={"url": "google.io"})

    def test_get_analytics(self):
        analytics = self.shared1.get_analytics()
        self.assertEqual(analytics["numberViews"], 3)
        self.assertEqual(
            analytics["outboundClicks"],
            [{"url": "proppy.io", "count": 2}, {"url": "google.io", "count": 1}]
        )

        sessions = analytics["sessions"]
        self.assertEqual(len(sessions), 3)

        self.assertEqual([s["length"] for s in sessions], [55, 40, 30])

        analytics = self.shared2.get_analytics()
        self.assertEqual(analytics["numberViews"], 2)
        self.assertEqual(analytics["outboundClicks"], [{"url": "google.io", "count": 1}])

        sessions = analytics["sessions"]
        self.assertEqual(len(sessions), 2)

        self.assertEqual([s["length"] for s in sessions], [15, 1])
Пример #3
0
    def test_get_latest_version(self):
        shared2 = SharedProposalFactory(proposal=self.p,
                                        version=self.shared.version + 1)
        data, status = self.get_json(self.url, user=self.user)

        self.assertEqual(status, 200)
        self.assertEqual(data["shared"]["version"], shared2.version)
Пример #4
0
    def test_cannot_duplicate_any_random_shared_proposal(self):
        proposal = ProposalFactory(company=self.waw)
        self.shared = SharedProposalFactory(proposal=proposal)

        _, status = self.post_json(
            "/templates/{}/duplicate".format(proposal.share_uid),
            {},
            user=self.user
        )
        self.assertEqual(status, 404)
Пример #5
0
 def test_can_share_draft_if_at_limit_for_plan(self):
     self.user.company.created_at = datetime.utcnow() - timedelta(days=60)
     ChargebeeSubscriptionCacheFactory(company=self.user.company)
     # Creating 4 sent proposals
     for _ in range(0, 4):
         SharedProposalFactory(
             version=1,
             proposal=ProposalFactory(company=self.user.company,
                                      status="sent"))
     _, status = self.post_json(self.url, {}, user=self.user)
     self.assertEqual(status, 200)
Пример #6
0
 def test_email_share_proposal_no_duplicate_contacts(self):
     SharedProposalFactory(version=1, proposal=self.p)
     with mail.record_messages() as outbox:
         self.c.contacts = ["*****@*****.**"]
         _, status = self.post_json(self.email_url,
                                    self.email,
                                    user=self.user)
         self.assertEqual(status, 200)
         self.assertListEqual(sorted(list(outbox[0].send_to)),
                              sorted(["*****@*****.**", self.user.email]))
         self.assertEqual(len(self.p.client.contacts), 1)
Пример #7
0
    def test_can_duplicate_template(self):
        self.shared = SharedProposalFactory(proposal=self.template, title=self.template.title)
        _, status = self.post_json(
            "/templates/{}/duplicate".format(self.template.share_uid),
            {},
            user=self.user
        )
        self.assertEqual(status, 201)

        company_proposals = self.user.company.proposals.all()

        self.assertEqual(1, len(company_proposals))
        self.assertEqual(company_proposals[0].title, self.template.title)
Пример #8
0
class TestAccessSharedProposal(DatabaseTest):
    def setUp(self):
        super(TestAccessSharedProposal, self).setUp()
        self.user = UserFactory()
        self.p = ProposalFactory(company=self.user.company)
        self.shared = SharedProposalFactory(proposal=self.p)
        self.url = "/shared/{}".format(self.p.share_uid)

    def test_access_404(self):
        _, status = self.get_json("/shared/42")
        self.assertEqual(status, 404)

    def test_access_proposal_not_shared(self):
        p = ProposalFactory(company=self.user.company)
        _, status = self.get_json("/shared/{}".format(p.share_uid))
        self.assertEqual(status, 404)

    def test_access_anonymous(self):
        data, status = self.get_json(self.url)

        self.assertEqual(status, 200)
        self.assertEqual(self.shared.to_json(), data["shared"])

    def test_access_logged_in(self):
        data, status = self.get_json(self.url, user=self.user)

        self.assertEqual(status, 200)
        self.assertEqual(self.shared.to_json(), data["shared"])

    def test_get_latest_version(self):
        shared2 = SharedProposalFactory(proposal=self.p,
                                        version=self.shared.version + 1)
        data, status = self.get_json(self.url, user=self.user)

        self.assertEqual(status, 200)
        self.assertEqual(data["shared"]["version"], shared2.version)
Пример #9
0
    def test_email_share_proposal(self):
        SharedProposalFactory(version=1, proposal=self.p)
        with mail.record_messages() as outbox:
            _, status = self.post_json(self.email_url,
                                       self.email,
                                       user=self.user)

            self.assertEqual(status, 200)
            self.assertEqual(self.p.shared_proposals.count(), 1)
            shared = self.p.shared_proposals.first()
            self.assertEqual(shared.version, 1)
            self.assertEqual(shared.blocks.count(), self.p.blocks.count())
            self.assertEqual(len(outbox), 1)
            self.assertIn(self.p.share_uid, outbox[0].body)
            self.assertIn("Yolo", outbox[0].body)
            self.assertEqual(len(self.p.client.contacts), 1)
            self.assertEqual(self.p.client.contacts[0], "*****@*****.**")
            self.assertEqual(shared.subject, self.email["subject"])
            self.assertEqual(shared.from_name, self.email["from"])
            self.assertEqual(shared.body, self.email["body"])
Пример #10
0
    def run(self):
        """
        The data here is as mix of randomized and hardcoded to
        capture a few corner cases (long names etc).
        """
        db = setup.db

        for table in reversed(db.metadata.sorted_tables):
            db.engine.execute(table.delete())

        company = CompanyFactory(name="A Cool Agency")
        user = UserFactory(id=None,
                           company=company,
                           email="*****@*****.**",
                           is_admin=True)
        db.session.add(user)
        db.session.commit()

        # we generate proposals with share uids of the templates so we can test
        # duplication locally
        for i in range(4):
            title = "Proposal for %s" % f.company()
            p = DefaultProposalFactory(company=company,
                                       title=title,
                                       client=None,
                                       share_uid=PROPOSAL_TEMPLATES[i]["uid"])
            db.session.add(p)
            s = SharedProposalFactory(proposal=p, version=1)
            db.session.add(s)

        # Super long title
        p = DefaultProposalFactory(
            company=company,
            title="long " * 12,
            client=None,
        )
        db.session.add(p)

        db.session.commit()
Пример #11
0
    def test_cant_share_if_signed(self):
        shared = SharedProposalFactory(version=1, proposal=self.p)
        SignatureFactory(proposal=self.p, shared_proposal=shared)
        _, code = self.post_json("/shared/%s/sign" % self.p.share_uid, {})

        self.assertEqual(code, 400)
Пример #12
0
 def test_share_mark_as_sent(self):
     SharedProposalFactory(version=1, proposal=self.p)
     self.assertEqual(self.p.status, "draft")
     _, status = self.post_json(self.sent_url, {}, user=self.user)
     self.assertEqual(status, 200)
     self.assertEqual(self.p.status, "sent")
Пример #13
0
 def setUp(self):
     super(TestAccessSharedProposal, self).setUp()
     self.user = UserFactory()
     self.p = ProposalFactory(company=self.user.company)
     self.shared = SharedProposalFactory(proposal=self.p)
     self.url = "/shared/{}".format(self.p.share_uid)
Пример #14
0
 def test_get_shared_emails(self):
     SharedProposalFactory(proposal=self.p, sent_to=["*****@*****.**", "*****@*****.**"])
     data, _ = self.get_json("/proposals/%d" % self.p.id, user=self.user)
     self.assertEqual(data["proposal"]["shares"][0]["sentTo"],
                      ["*****@*****.**", "*****@*****.**"])
Пример #15
0
 def setUp(self):
     super(TestSharedCommenting, self).setUp()
     self.user = UserFactory()
     self.p = DefaultProposalFactory(company=self.user.company)
     self.shared = SharedProposalFactory(proposal=self.p)
     self.url = "/shared/{}/comments".format(self.p.share_uid)