Example #1
0
class TestCheckUnverifiedQuest(TestCase):

    def setUp(self):
        settings.CELERY_ALWAYS_EAGER = True
        self.email = "*****@*****.**"
        create_user_util_test(self.email)
        self.pleb = Pleb.nodes.get(email=self.email)
        self.user = User.objects.get(email=self.email)
        self.quest = Quest(owner_username=self.pleb.username,
                           object_uuid=str(uuid1())).save()
        self.quest.owner.connect(self.pleb)

    def tearDown(self):
        settings.CELERY_ALWAYS_EAGER = False

    @requests_mock.mock()
    def test_less_than_three_days(self, m):
        m.post("https://api.intercom.io/events/",
               json={
                   "event_name": "unverified-quest",
                   "user_id": self.quest.owner_username
               },
               status_code=status.HTTP_202_ACCEPTED)
        now = datetime.now(pytz.utc)
        self.quest.account_first_updated = now
        self.quest.save()
        res = check_unverified_quest.apply_async()
        while not res.ready():
            time.sleep(1)
        self.assertTrue(res.result)

    @requests_mock.mock()
    def test_greater_than_three_days(self, m):
        m.post("https://api.intercom.io/events/",
               json={
                   "event_name": "still-unverified-quest",
                   "user_id": self.quest.owner_username
               },
               status_code=status.HTTP_202_ACCEPTED)
        past = datetime.now(pytz.utc) - relativedelta(days=4)
        self.quest.account_first_updated = past
        self.quest.account_verified = "unverified"
        self.quest.save()
        res = check_unverified_quest.apply_async()
        while not res.ready():
            time.sleep(1)
        self.assertTrue(res.result)
class TestOrderTasks(TestCase):
    def setUp(self):
        query = 'MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)
        self.email = "*****@*****.**"
        self.password = "******"
        self.pleb = create_user_util_test(self.email)
        self.user = User.objects.get(email=self.email)
        self.pleb.email_verified = True
        self.pleb.save()
        cache.clear()
        self.mission = Mission(owner_username=self.user.username,
                               title=str(uuid1())).save()
        self.quest = Quest(owner_username=self.pleb.username).save()
        self.quest.missions.connect(self.mission)
        self.quest.owner.connect(self.pleb)

    def test_uncompleted_sort(self):
        setup_onboarding(self.quest, self.mission)
        db.cypher_query('MATCH (a:OnboardingTask {title: "Mission Setup"}) '
                        'SET a.completed=False RETURN a')
        uncompleted_sort, completed_count = order_tasks(
            self.mission.object_uuid)
        self.assertEqual(len(uncompleted_sort), len(settings.ONBOARDING_TASKS))
        for item in range(0, len(settings.ONBOARDING_TASKS) - 1):
            self.assertEqual(uncompleted_sort[item]['priority'], item + 1)

    def test_completed_sort_none_completed(self):
        setup_onboarding(self.quest, self.mission)
        db.cypher_query('MATCH (a:OnboardingTask {title: "Mission Setup"}) '
                        'SET a.completed=False RETURN a')
        uncompleted_sort, completed_count = order_tasks(
            self.mission.object_uuid)
        self.assertEqual(completed_count, 0)

    def test_completed_sort_one_completed(self):
        self.quest.account_verified = "verified"
        self.quest.save()
        setup_onboarding(self.quest, self.mission)
        db.cypher_query('MATCH (a:OnboardingTask {title: "Mission Setup"}) '
                        'SET a.completed=False RETURN a')
        uncompleted_sort, completed_count = order_tasks(
            self.mission.object_uuid)
        self.assertEqual(completed_count, 1)
        self.assertTrue(
            uncompleted_sort[len(uncompleted_sort) - 1]['completed'])
class TestSetupOnboarding(TestCase):
    def setUp(self):
        query = 'MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
        db.cypher_query(query)

        self.email = "*****@*****.**"
        self.password = "******"
        self.pleb = create_user_util_test(self.email)
        self.user = User.objects.get(email=self.email)
        self.pleb.email_verified = True
        self.pleb.save()
        cache.clear()
        self.mission = Mission(owner_username=self.user.username,
                               title=str(uuid1())).save()
        self.quest = Quest(owner_username=self.pleb.username).save()
        self.quest.missions.connect(self.mission)
        self.quest.owner.connect(self.pleb)

    def test_verify_same_onboarding_task_list(self):
        temp_list = copy.deepcopy(settings.ONBOARDING_TASKS)
        setup_onboarding(self.quest, self.mission)
        self.assertEqual(temp_list, settings.ONBOARDING_TASKS)

    def test_set_wallpaper(self):
        self.quest.wallpaper_pic = "something.png"
        self.quest.save()
        setup_onboarding(self.quest, self.mission)
        query = 'MATCH (a:Mission {object_uuid: "%s"})-[:MUST_COMPLETE]->' \
                '(task:OnboardingTask {title: "%s"}) RETURN task' % (
                    self.mission.object_uuid, settings.QUEST_WALLPAPER_TITLE)
        res, _ = db.cypher_query(query)
        self.assertTrue(res.one['completed'])

    def test_set_bank_setup(self):
        self.quest.account_verified = "verified"
        self.quest.save()
        setup_onboarding(self.quest, self.mission)
        query = 'MATCH (a:Mission {object_uuid: "%s"})-[:MUST_COMPLETE]->' \
                '(task:OnboardingTask {title: "%s"}) RETURN task' % (
                    self.mission.object_uuid, settings.BANK_SETUP_TITLE)
        res, _ = db.cypher_query(query)
        self.assertTrue(res.one['completed'])

    def test_set_quest_about(self):
        self.quest.about = "some short summary"
        self.quest.save()
        setup_onboarding(self.quest, self.mission)
        query = 'MATCH (a:Mission {object_uuid: "%s"})-[:MUST_COMPLETE]->' \
                '(task:OnboardingTask {title: "%s"}) RETURN task' % (
                    self.mission.object_uuid, settings.QUEST_ABOUT_TITLE)
        res, _ = db.cypher_query(query)
        self.assertTrue(res.one['completed'])
Example #4
0
class AccountingHooksTests(APITestCase):
    def setUp(self):
        query = "MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r"
        db.cypher_query(query)
        try:
            self.pleb2 = Pleb.nodes.get(
                username=settings.INTERCOM_USER_ID_DEVON)
        except (Pleb.DoesNotExist, DoesNotExist):
            self.pleb2 = Pleb(username=settings.INTERCOM_USER_ID_DEVON).save()
        self.unit_under_test_name = 'accounting'
        self.email = "*****@*****.**"
        self.pleb = create_user_util_test(self.email)
        self.user = User.objects.get(email=self.email)
        self.url = "http://testserver"
        self.quest = Quest(about='Test Bio',
                           owner_username=self.pleb.username).save()
        self.quest.editors.connect(self.pleb)
        self.quest.moderators.connect(self.pleb)
        cache.clear()
        self.stripe = stripe
        self.stripe.api_key = settings.STRIPE_SECRET_KEY
        stripe.api_version = settings.STRIPE_API_VERSION
        self.intercom_url = "https://api.intercom.io/admins"

    def tearDown(self):
        self.pleb2.delete()
        cache.clear()

    def test_invalid_event_request(self):
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {"id": "evt_00000000000000"}
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    @requests_mock.mock()
    def test_valid_event_request(self, m):
        intercom_mock_data = {
            "type":
            "admin.list",
            "admins": [{
                "type": "admin",
                "id": "69989",
                "name": "Devon Bleibtrey",
                "email": "*****@*****.**"
            }]
        }
        m.get(self.intercom_url,
              json=intercom_mock_data,
              status_code=status.HTTP_200_OK)
        customer_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/customers/cus_00000000000000",
              json=customer_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "invoice.payment_failed",
            "data": {
                "object": {
                    "customer": "cus_00000000000000"
                }
            }
        }
        m.post("https://api.intercom.io/messages/",
               status_code=status.HTTP_202_ACCEPTED)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['detail'], "Invoice Payment Failed")

    @requests_mock.mock()
    def test_invoice_payment_failed_request_no_user(self, m):
        customer_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/customers/cus_00000000000000",
              json=customer_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "invoice.payment_failed",
            "data": {
                "object": {
                    "customer": "cus_00000000000000"
                }
            }
        }
        query = 'MATCH (pleb:Pleb {email: "%s"})-[r]-() ' \
                'DELETE pleb, r' % "*****@*****.**"
        db.cypher_query(query)
        m.post("https://api.intercom.io/messages/",
               status_code=status.HTTP_202_ACCEPTED)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['id'], "evt_00000000000000")

    @requests_mock.mock()
    def test_valid_event_request_invalid_serializer(self, m):
        intercom_mock_data = {
            "type":
            "admin.list",
            "admins": [{
                "type": "admin",
                "id": "123456",
                "name": "Devon Bleibtrey",
                "email": "*****@*****.**"
            }]
        }
        m.get(self.intercom_url,
              json=intercom_mock_data,
              status_code=status.HTTP_200_OK)
        customer_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/customers/cus_00000000000000",
              json=customer_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "invoice.payment_failed",
            "data": {
                "object": {
                    "customer": "cus_00000000000000"
                }
            }
        }
        m.post("https://api.intercom.io/messages/",
               status_code=status.HTTP_202_ACCEPTED)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['from_user'],
                         ["69989 is not a valid admin ID"])

    @requests_mock.mock()
    def test_valid_event_invalid_customer_request(self, m):
        customer_mock_data = {"error": {"type": "invalid_request_error"}}
        m.get("https://api.stripe.com/v1/customers/cus_00000000000000",
              json=customer_mock_data,
              status_code=status.HTTP_400_BAD_REQUEST)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "invoice.payment_failed",
            "data": {
                "object": {
                    "customer": "cus_00000000000000"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    @requests_mock.mock()
    def test_valid_event_request_account_updated(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        account_mock_data = {
            "email": "*****@*****.**",
            "verification": {
                "disabled_reason": "fields_needed",
                "due_by": None,
                "fields_needed": ["legal_entity.type"]
            },
            "legal_entity": {
                "verification": {
                    "status": "verified",
                    "details": "Identity document is too unclear to read."
                }
            }
        }
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000000",
              json=account_mock_data,
              status_code=status.HTTP_200_OK)

        admin_mock_data = {
            "type":
            "admin.list",
            "admins": [{
                "type": "admin",
                "id": settings.INTERCOM_ADMIN_ID_DEVON,
                "name": "Devon Bleibtrey",
                "email": "*****@*****.**"
            }]
        }
        m.get("https://api.intercom.io/admins",
              json=admin_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "account.updated",
            "data": {
                "object": {
                    "id": "acct_00000000000000"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['detail'], "Account Updated")

    @requests_mock.mock()
    def test_no_user_account_updated(self, m):
        account_mock_data = {
            "email": "*****@*****.**",
            "verification": {
                "disabled_reason": "fields_needed",
                "due_by": None,
                "fields_needed": ["legal_entity.type"]
            },
            "legal_entity": {
                "verification": {
                    "status": "verified",
                    "details": "Identity document is too unclear to read."
                }
            }
        }
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000000",
              json=account_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "account.updated",
            "data": {
                "object": {
                    "id": "acct_00000000000000"
                }
            }
        }
        query = 'MATCH (pleb:Pleb {email: "%s"})-[r]-() ' \
                'DELETE pleb, r' % "*****@*****.**"
        db.cypher_query(query)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['id'], "evt_00000000000000")

    @requests_mock.mock()
    def test_valid_event_request_account_updated_invalid_account(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        account_mock_data = {
            "email": "*****@*****.**",
            "verification": {
                "disabled_reason": None,
                "due_by": None,
                "fields_needed": []
            },
            "legal_entity": {
                "verification": {
                    "status": "verified",
                    "details": "Identity document is too unclear to read."
                }
            }
        }
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000001",
              json=account_mock_data,
              status_code=status.HTTP_200_OK)

        admin_mock_data = {
            "type":
            "admin.list",
            "admins": [{
                "type": "admin",
                "id": settings.INTERCOM_ADMIN_ID_DEVON,
                "name": "Devon Bleibtrey",
                "email": "*****@*****.**"
            }]
        }
        m.get("https://api.intercom.io/admins",
              json=admin_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "account.updated",
            "data": {
                "object": {
                    "id": "acct_00000000000001"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    @requests_mock.mock()
    def test_valid_event_request_account_updated_deleted(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        account_mock_data = {"id": "acct_00000000000000", "deleted": True}
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000000",
              json=account_mock_data,
              status_code=status.HTTP_200_OK)
        admin_mock_data = {
            "type":
            "admin.list",
            "admins": [{
                "type": "admin",
                "id": settings.INTERCOM_ADMIN_ID_DEVON,
                "name": "Devon Bleibtrey",
                "email": "*****@*****.**"
            }]
        }
        m.get("https://api.intercom.io/admins",
              json=admin_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "account.updated",
            "data": {
                "object": {
                    "id": "acct_00000000000000"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.data, data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    @requests_mock.mock()
    def test_valid_event_request_transfer_failed_stripe_account(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        transfer_mock_data = {
            "type": "stripe_account",
            "destination": "acct_00000000000000"
        }
        m.get("https://api.stripe.com/v1/transfers/tr_00000000000000",
              json=transfer_mock_data,
              status_code=status.HTTP_200_OK)
        account_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000000",
              json=account_mock_data,
              status_code=status.HTTP_201_CREATED)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "transfer.failed",
            "data": {
                "object": {
                    "id": "tr_00000000000000"
                }
            }
        }
        intercom_mock_data = {
            "event_name": "stripe-transfer-failed",
            "user_id": self.pleb.username,
            "created": calendar.timegm(datetime.now(pytz.utc).utctimetuple())
        }
        m.post("https://api.intercom.io/events/",
               json=intercom_mock_data,
               status_code=status.HTTP_202_ACCEPTED)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['detail'], "Transfer Failed")

    @requests_mock.mock()
    def test_no_user_transfer_failed_stripe_account(self, m):
        transfer_mock_data = {
            "type": "stripe_account",
            "destination": "acct_00000000000000"
        }
        m.get("https://api.stripe.com/v1/transfers/tr_00000000000000",
              json=transfer_mock_data,
              status_code=status.HTTP_200_OK)
        account_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000000",
              json=account_mock_data,
              status_code=status.HTTP_201_CREATED)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "transfer.failed",
            "data": {
                "object": {
                    "id": "tr_00000000000000"
                }
            }
        }
        query = 'MATCH (pleb:Pleb {email: "%s"})-[r]-() ' \
                'DELETE pleb, r' % "*****@*****.**"
        db.cypher_query(query)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['id'], "evt_00000000000000")

    @requests_mock.mock()
    def test_valid_event_request_transfer_failed_account_deleted(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        transfer_mock_data = {
            "type": "stripe_account",
            "destination": "acct_00000000000000"
        }
        m.get("https://api.stripe.com/v1/transfers/tr_00000000000000",
              json=transfer_mock_data,
              status_code=status.HTTP_200_OK)
        account_mock_data = {"id": "acct_00000000000000", "deleted": True}
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000000",
              json=account_mock_data,
              status_code=status.HTTP_201_CREATED)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "transfer.failed",
            "data": {
                "object": {
                    "id": "tr_00000000000000"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data, data)

    @requests_mock.mock()
    def test_transfer_failed_stripe_account_invalid_transfer(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        transfer_mock_data = {
            "type": "stripe_account",
            "destination": "acct_00000000000000"
        }
        m.get("https://api.stripe.com/v1/transfers/tr_00000000000001",
              json=transfer_mock_data,
              status_code=status.HTTP_200_OK)
        account_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/accounts/acct_00000000000000",
              json=account_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "transfer.failed",
            "data": {
                "object": {
                    "id": "tr_00000000000001"
                }
            }
        }
        intercom_mock_data = {
            "event_name": "stripe-transfer-failed",
            "user_id": self.pleb.username,
            "created": calendar.timegm(datetime.now(pytz.utc).utctimetuple())
        }
        m.post("https://api.intercom.io/events/",
               json=intercom_mock_data,
               status_code=status.HTTP_202_ACCEPTED)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    @requests_mock.mock()
    def test_valid_event_request_trial_will_end(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        customer_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/customers/cus_00000000000000",
              json=customer_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "customer.subscription.trial_will_end",
            "data": {
                "object": {
                    "customer": "cus_00000000000000"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['detail'], "Trail Will End")

    @requests_mock.mock()
    def test_no_user_request_trial_will_end(self, m):
        customer_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/customers/cus_00000000000000",
              json=customer_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "customer.subscription.trial_will_end",
            "data": {
                "object": {
                    "customer": "cus_00000000000000"
                }
            }
        }
        query = 'MATCH (pleb:Pleb {email: "%s"})-[r]-() ' \
                'DELETE pleb, r' % "*****@*****.**"
        db.cypher_query(query)
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['id'], "evt_00000000000000")

    @requests_mock.mock()
    def test_valid_event_request_trial_will_end_invalid_customer(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        customer_mock_data = {"email": "*****@*****.**"}
        m.get("https://api.stripe.com/v1/customers/cus_00000000000001",
              json=customer_mock_data,
              status_code=status.HTTP_200_OK)
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "customer.subscription.trial_will_end",
            "data": {
                "object": {
                    "customer": "cus_00000000000001"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    @requests_mock.mock()
    def test_invalid_event_type(self, m):
        self.quest.account_verified = "unverified"
        self.quest.save()
        cache.clear()
        self.client.force_authenticate(user=self.user)
        url = reverse('accounting-list')
        data = {
            "id": "evt_00000000000000",
            "type": "some.invalid.thing",
            "data": {
                "object": {
                    "customer": "cus_00000000000001"
                }
            }
        }
        response = self.client.post(url, data=data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
class QuestViewTests(TestCase):
    def setUp(self):
        db.cypher_query('MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r')
        self.factory = APIRequestFactory()
        self.client = Client()
        self.email = "*****@*****.**"
        self.password = "******"
        create_user_util_test(self.email)
        self.pleb = Pleb.nodes.get(email=self.email)
        self.user = User.objects.get(email=self.email)
        self.pleb.email_verified = True
        self.pleb.save()
        cache.clear()
        self.mission = Mission(owner_username=self.user.username,
                               title=str(uuid1()),
                               focus_on_type="advocacy",
                               focus_name="testing advocacy",
                               district=None).save()
        self.quest = Quest(owner_username=self.pleb.username).save()
        self.quest.missions.connect(self.mission)
        self.quest.owner.connect(self.pleb)
        setup_onboarding(self.quest, self.mission)

    def test_quest(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest', kwargs={'username': self.user.username})
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_quest_dne(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest', kwargs={'username': str(uuid1())})
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_quest_about_is_none(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest', kwargs={'username': self.user.username})
        self.quest.about = None
        self.quest.save()
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_quest_about_is_not_none(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest', kwargs={'username': self.user.username})
        self.quest.about = "Some Test Stuff!"
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_quest_settings(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest_manage_settings',
                      kwargs={'username': self.user.username})
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_quest_settings_with_address(self):
        address = Address(street="3295 Rio Vista St",
                          city="Commerce Township",
                          state="MI",
                          postal_code="48382",
                          country="US",
                          congressional_district="11").save()
        self.quest.address.connect(address)
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest_manage_settings',
                      kwargs={'username': self.user.username})
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_settings_no_quest(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest_manage_settings',
                      kwargs={'username': self.user.username})
        query = "MATCH (n) OPTIONAL MATCH " \
                "(n)-[r]-() DELETE n, r"
        res, _ = db.cypher_query(query)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_settings_no_mission(self):
        db.cypher_query('MATCH (a:Mission) OPTIONAL MATCH (a)-[r]-()'
                        ' DELETE a, r')
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('quest_manage_settings',
                      kwargs={'username': self.user.username})
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)
Example #6
0
    def create_prepopulated_reps(self):
        count = 0
        while True:
            query = 'MATCH (role:GTRole) ' \
                    'RETURN role SKIP %s LIMIT 25' % count
            res, _ = db.cypher_query(query)
            if not res.one:
                break
            count += 24
            for role in [GTRole.inflate(row[0]) for row in res]:
                if role.current:
                    query = 'MATCH (role:GTRole {role_id: %s})' \
                            '-[:IS]->(person:GTPerson) ' \
                            'RETURN person' % role.role_id
                    res, _ = db.cypher_query(query)
                    if res.one is None:
                        continue
                    else:
                        person = GTPerson.inflate(res.one)

                    try:
                        rep = PublicOfficial.nodes.get(gt_id=person.gt_id)
                    except (DoesNotExist, PublicOfficial.DoesNotExist):
                        try:
                            state = dict(US_STATES)[role.state]
                        except KeyError:
                            state = role.state
                        rep = PublicOfficial(first_name=person.firstname,
                                             last_name=person.lastname,
                                             gender=person.gender,
                                             date_of_birth=person.birthday,
                                             name_mod=person.namemod,
                                             current=role.current,
                                             bio=role.description,
                                             district=role.district,
                                             state=state,
                                             title=role.title,
                                             website=role.website,
                                             start_date=role.startdate,
                                             end_date=role.enddate,
                                             full_name=person.name,
                                             twitter=person.twitterid,
                                             youtube=person.youtubeid,
                                             gt_id=person.gt_id,
                                             gov_phone=role.phone,
                                             bioguideid=person.bioguideid)
                        rep.save()
                    except (CypherException, IOError) as e:
                        logger.exception(e)
                        continue
                    quest = rep.get_quest()
                    if not quest:
                        quest = Quest(
                            about=rep.bio,
                            youtube=rep.youtube,
                            twitter=rep.twitter,
                            website=rep.website,
                            first_name=rep.first_name,
                            last_name=rep.last_name,
                            owner_username=rep.object_uuid,
                            title="%s %s" % (rep.first_name, rep.last_name),
                            profile_pic="%s/representative_images/225x275/"
                            "%s.jpg" % (settings.LONG_TERM_STATIC_DOMAIN,
                                        rep.bioguideid)).save()
                        rep.quest.connect(quest)
                    else:
                        quest.profile_pic = \
                            "%s/representative_images/225x275/" \
                            "%s.jpg" % (settings.LONG_TERM_STATIC_DOMAIN,
                                        rep.bioguideid)
                        quest.save()
                        cache.set('%s_quest' % quest.object_uuid, quest)
                    rep.gt_person.connect(person)
                    rep.gt_role.connect(role)
                    task_data = {
                        "object_uuid": quest.object_uuid,
                        "label": 'quest',
                    }
                    spawn_task(update_search_object, task_data)
        populate_term_data()
class MissionViewTests(TestCase):
    def setUp(self):
        db.cypher_query('MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r')
        self.factory = APIRequestFactory()
        self.client = Client()
        self.email = "*****@*****.**"
        self.password = "******"
        create_user_util_test(self.email)
        self.pleb = Pleb.nodes.get(email=self.email)
        self.user = User.objects.get(email=self.email)
        self.pleb.email_verified = True
        self.pleb.save()
        cache.clear()
        self.mission = Mission(owner_username=self.user.username,
                               title=str(uuid1())).save()
        self.quest = Quest(owner_username=self.pleb.username).save()
        self.quest.missions.connect(self.mission)
        self.quest.owner.connect(self.pleb)

    def test_mission_list(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_list')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_mission_selector(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('select_mission')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_public_office(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('public_office')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_advocate_mission(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('advocate')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_account_setup(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('account_setup')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_account_setup_not_logged_in(self):
        url = reverse('account_setup')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_account_setup_no_quest(self):
        db.cypher_query('MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r')
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('account_setup')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_mission_redirect(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_redirect',
                      kwargs={'object_uuid': self.mission.object_uuid})
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_301_MOVED_PERMANENTLY)

    def test_mission_redirect_dne(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_redirect', kwargs={'object_uuid': str(uuid1())})
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_mission(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_mission_dne(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission',
                      kwargs={
                          'object_uuid': str(uuid1()),
                          'slug': str(uuid1())
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_mission_updates(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_updates',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        update = Update(title=str(uuid1())).save()
        update.mission.connect(self.mission)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_mission_updates_dne(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_updates',
                      kwargs={
                          'object_uuid': str(uuid1()),
                          'slug': str(uuid1())
                      })
        update = Update(title=str(uuid1())).save()
        update.mission.connect(self.mission)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_mission_updates_no_updates(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_updates',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_mission_updates_no_quest(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_updates',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        update = Update(title=str(uuid1())).save()
        update.mission.connect(self.mission)
        self.quest.missions.disconnect(self.mission)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_mission_settings_redirect(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_settings_redirect')
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_mission_settings(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_settings',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_mission_settings_completed_wallpaper(self):
        self.quest.wallpaper_pic = "helloworld.png"
        self.quest.save()
        setup_onboarding(self.quest, self.mission)
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_settings',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_mission_settings_completed_verification(self):
        self.quest.account_verified = "verified"
        self.quest.save()
        setup_onboarding(self.quest, self.mission)
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_settings',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_mission_settings_completed_about(self):
        self.quest.about = "some short summary"
        self.quest.save()
        setup_onboarding(self.quest, self.mission)
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_settings',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_endorse(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_endorse',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_endorse_unauthorized(self):
        url = reverse('mission_endorse',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)

    def test_endorsements(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_endorsements',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_endorsements_unauthorized(self):
        url = reverse('mission_endorsements',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_conversations(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_conversations',
                      kwargs={
                          'object_uuid': self.mission.object_uuid,
                          'slug': self.mission.get_mission_title()
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

    def test_conversations_does_not_exist(self):
        self.client.login(username=self.user.username, password=self.password)
        url = reverse('mission_conversations',
                      kwargs={
                          'object_uuid': str(uuid1()),
                          'slug': str(uuid1())
                      })
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_302_FOUND)