def test_view_order_history_for_subscription(self):
        self.grantor_org = self.user.organisation
        order = OrderFactory.create(organisation=self.grantor_org,
                                    status=Order.STATUS_APPROVED)

        SubscriptionFactory.create(order=order)

        invitation_count = 1
        AssessmentPurchaseFactory.create(
            package=self.package,
            order=order,
            order__organisation=self.grantor_org,
            order__status=Order.STATUS_APPROVED,
            number_included=invitation_count,
        )

        with self.wait_for_page_load():
            self.browser.find_element_by_link_text(
                "Subscription & billing").click()
        with self.wait_for_page_load():
            self.browser.find_element_by_link_text("Order history").click()

        table_order = self.browser.find_element_by_id("packages-list")

        table_rows = table_order.find_elements_by_xpath("tbody/tr/td")
        current_formatted_date = date.today().strftime("%d %b %Y")
        self.assertEqual(current_formatted_date.lstrip("0"),
                         table_rows[1].text)
        self.assertEquals("Paid", table_rows[2].text)
    def test_option_for_user_to_renew_subscription_on_dashboard(self):
        self.grantor_org = self.user.organisation
        order = OrderFactory.create(organisation=self.grantor_org,
                                    status=Order.STATUS_APPROVED)

        SubscriptionFactory.create(order=order, end_date=date.today())

        invitation_count = 1
        AssessmentPurchaseFactory.create(
            package=self.package,
            order=order,
            order__organisation=self.grantor_org,
            order__status=Order.STATUS_APPROVED,
            number_included=invitation_count,
        )

        with self.wait_for_page_load():
            self.browser.find_element_by_link_text("Dashboard").click()

        expired_subscription_message = "Your subscription expires in 0 days"
        expired_subscription_element = self.browser.find_element_by_css_selector(
            "div > .col-md-6.mt-3 > .text-bold")

        self.assertIn(expired_subscription_message,
                      expired_subscription_element.text)

        renew_button = self.browser.find_element_by_link_text("Renew")
        self.assertTrue(renew_button.is_enabled())
    def test_user_should_be_able_to_view_remaining_invitation_credits(self):
        self.grantor_org = self.user.organisation
        order = OrderFactory.create(organisation=self.grantor_org,
                                    status=Order.STATUS_APPROVED)

        SubscriptionFactory.create(order=order)

        invitation_count = 10
        AssessmentPurchaseFactory.create(
            package=self.package,
            order=order,
            order__organisation=self.grantor_org,
            order__status=Order.STATUS_APPROVED,
            number_included=invitation_count,
        )

        with self.wait_for_page_load():
            self.browser.find_element_by_link_text("Invitations").click()

        # credits_field = self.browser_wait.until(
        #     lambda browser: browser.find_element_by_class_name(
        #         "badge-pill.bg-crystal-blue.number")
        # )

        # locator changed and WebDriverWait works
        wait = WebDriverWait(self.browser, 10)
        credits_field = wait.until(
            EC.presence_of_element_located((
                By.XPATH,
                "//span[@class='badge badge-pill bg-crystal-blue _700 number mx-1']"
            )))
        actual_text = credits_field.text
        self.assertEquals(actual_text, "10")
        self.assertEqual(order.organisation.remaining_invites, 10)
    def test_user_cannot_send_invitation_where_there_are_no_credits_left(self):
        self.grantor_org = self.user.organisation
        order = OrderFactory.create(organisation=self.grantor_org,
                                    status=Order.STATUS_APPROVED)

        SubscriptionFactory.create(order=order)

        invitation_count = 1
        AssessmentPurchaseFactory.create(
            package=self.package,
            order=order,
            order__organisation=self.grantor_org,
            order__status=Order.STATUS_APPROVED,
            number_included=invitation_count,
        )
        self.grantee_user = UserFactory.create(password=self.password)
        assign_role(self.grantee_user, 'manager')

        with self.wait_for_page_load():
            self.browser.find_element_by_link_text("Invitations").click()
        # credits_field = self.browser_wait.until(
        #     lambda browser: browser.find_element_by_class_name(
        #         "badge-pill.bg-crystal-blue.number")
        # )

        wait = WebDriverWait(self.browser, 10)
        credits_field = wait.until(
            EC.presence_of_element_located((
                By.XPATH,
                "//span[@class='badge badge-pill bg-crystal-blue _700 number mx-1']"
            )))
        actual_text = credits_field.text
        self.assertEquals(actual_text, "1")
        self.assertEqual(order.organisation.remaining_invites, 1)

        with self.wait_for_page_load():
            self.browser.find_element_by_link_text("Make invitation").click()

        # select from list
        self.select_drop_down_for_invitation("grantee")

        # select survey
        self.select_drop_down_for_invitation("survey")

        # select tier
        # 0-Bronze,1-Silver,2-Gold,3-Platinum
        tiers = self.browser.find_elements_by_css_selector(
            "label[class='ui-check w-sm']")
        tiers[1].click()

        self.browser.find_element_by_css_selector(
            "button[type='submit']").click()

        db_order = Order.objects.get(id=order.id)
        self.assertEqual(db_order.organisation.remaining_invites, 0)
        self.assertTrue(
            EC.invisibility_of_element_located(
                (By.LINK_TEXT, "Make invitation")))
    def test_view_submitted_assessment_report_by_grantee(self):
        self.grantee = UserFactory.create(password=self.password)
        assign_role(self.grantee, "admin")
        assign_role(self.user, "admin")

        self.survey = SurveyFactory.create()

        self.survey_response = SurveyResponseFactory.create(
            organisation=self.grantee.organisation,
            survey=self.survey,
            level=1,
            submitted=timezone.now())
        self.section = SurveySectionFactory.create(number=1, )
        self.question = SurveyQuestionFactory.create(survey=self.survey,
                                                     level=1,
                                                     section=self.section)
        self.answer = SurveyAnswerFactory.create(response=self.survey_response,
                                                 question=self.question)
        self.invitation = InvitationFactory.create(
            survey=self.survey,
            grantee=self.survey_response.organisation,
            status=3,
            accepted=True,
            grantor=self.user.organisation)

        grantor_org = self.user.organisation
        order = OrderFactory.create(organisation=grantor_org,
                                    status=Order.STATUS_APPROVED)

        SubscriptionFactory.create(order=order)
        package = AssessmentPackageFactory.create(number_included=1,
                                                  price=900.00)
        AssessmentPurchaseFactory.create(
            package=package,
            order=order,
            order__organisation=grantor_org,
            order__status=Order.STATUS_APPROVED,
            number_included=1,
        )

        self.login(self.user.email, self.password)

        self.click_and_wait_for_page_load(
            self.browser.find_element_by_link_text("Invitations"), )
        self.assertTrue(EC.presence_of_element_located((By.LINK_TEXT, "View")))
        view_submitted_survey = self.browser.find_element_by_link_text("View")
        self.click_and_wait_for_page_load(view_submitted_survey)

        self.assertTrue(
            self.browser_wait.until(
                lambda browser: browser.find_element_by_link_text(
                    "Print report").is_enabled()))
        completion_percentage = self.browser_wait.until(
            lambda browser: self.browser.find_element_by_css_selector(
                ".number._700"))
        report_header = self.browser.find_element_by_tag_name("h2").text
        self.assertEquals(completion_percentage.text, "100%")
        self.assertIn("Full report", report_header)
 def test_order_confirmation_package(self):
     assign_role(self.user, 'admin')
     package = AssessmentPackageFactory.create(number_included=1,
                                               price=900.00)
     AssessmentPurchaseFactory.create(package=package,
                                      order=self.order,
                                      number_included=1,
                                      price=900.00)
     self.order.send_order_confirmation()
    def setUp(self):
        self.user = UserFactory.create(password=self.password)
        assign_role(self.user, 'manager')

        self.survey = SurveyFactory.create()

        self.section4_1 = SurveySectionFactory.create(number=1, )

        self.section4_2 = SurveySectionFactory.create(number=2, )

        self.section4_3 = SurveySectionFactory.create(number=3, )

        self.q1 = SurveyQuestionFactory.create(survey=self.survey,
                                               level=1,
                                               section=self.section4_1)

        self.q2 = SurveyQuestionFactory.create(survey=self.survey,
                                               level=2,
                                               section=self.section4_1)
        self.q3 = SurveyQuestionFactory.create(survey=self.survey,
                                               level=2,
                                               section=self.section4_1)

        self.q4 = SurveyQuestionFactory.create(survey=self.survey,
                                               level=3,
                                               section=self.section4_2)
        self.q5 = SurveyQuestionFactory.create(survey=self.survey,
                                               level=4,
                                               section=self.section4_3)

        self.grantor_org = self.user.organisation
        order = OrderFactory.create(organisation=self.grantor_org,
                                    status=Order.STATUS_APPROVED)

        SubscriptionFactory.create(order=order)
        package = AssessmentPackageFactory.create(number_included=1,
                                                  price=900.00)
        AssessmentPurchaseFactory.create(
            package=package,
            order=order,
            order__organisation=self.grantor_org,
            order__status=Order.STATUS_APPROVED,
            number_included=10,
        )

        self.login(self.user.email, self.password)
        self.assertIn(self.title, self.browser.title)
    def setUp(self):
        self.user = UserFactory.create(password=self.password)
        assign_role(self.user, 'admin')
        self.browser.get(self.live_server_url)

        self.grantor = self.user.organisation
        new_order = OrderFactory.create(organisation=self.grantor,
                                        status=Order.STATUS_APPROVED)

        SubscriptionFactory.create(order=new_order, price=1500.00)

        new_package = AssessmentPackageFactory.create(name=str(self.user),
                                                      number_included=2,
                                                      price=900.00)

        self.assess = AssessmentPurchaseFactory.create(order=new_order,
                                                       package=new_package,
                                                       number_included=2,
                                                       price=900.00)

        self.user1 = UserFactory.create(password=self.password)
        self.grantee = self.user1.organisation
        self.survey_list = SurveyFactory.create(name="Survey 0")