Ejemplo n.º 1
0
def test_staff_post_edit(client):
    user = f.create_user(email='*****@*****.**', password='******')
    client.login(user=user)

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': '2018-10-09T18:30:00Z'
    }
    response = client.json.post(url, json.dumps(post))
    assert response.status_code == 201
    post_id = response.data['id']

    user = f.create_user(email='*****@*****.**',
                         password='******',
                         is_staff=True)
    client.login(user=user)
    url = reverse('posts-detail', kwargs={'pk': post_id})
    data = {
        'text': 'Edited announcement',
    }
    response = client.json.patch(url, json.dumps(data))
    assert response.status_code == 200
    assert response.data["text"] == "Edited announcement"
Ejemplo n.º 2
0
def test_post_approved_api(client):
    user = f.create_user(email='*****@*****.**', password='******')
    client.login(user=user)

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': '2018-10-09T18:30:00Z'
    }
    response = client.json.post(url, json.dumps(post))
    assert response.status_code == 201
    post_id = response.data['id']

    # Check that a normal user is not able to approve post
    non_core_organizer = f.create_user(email='*****@*****.**',
                                       password='******')
    client.login(user=non_core_organizer)
    url = reverse('posts-approve', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 403

    core_organizer = f.create_user(is_core_organizer=True)
    client.login(user=core_organizer)
    url = reverse('posts-approve', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 200
    assert response.data['is_approved'] is True
    assert response.data['approval_time']

    # If post is already approved
    url = reverse('posts-approve', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 200
    assert response.data['is_approved'] is True
 def setUp(self):
     product = create_product()
     payment_product = create_payment_product(product=product)
     payment = payment_product.payment
     self.url = reverse('api:payment-product-by-payment-id',
                        kwargs=dict(payment=str(payment.id)))
     create_user()
Ejemplo n.º 4
0
def test_unapproving_post_not_allowed_to_non_core_organizer(client):
    non_core_organizer = f.create_user(email='*****@*****.**',
                                       password='******')
    client.login(user=non_core_organizer)

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': '2018-10-09T18:30:00Z'
    }
    response = client.json.post(url, json.dumps(post))
    assert response.status_code == 201
    post_id = response.data['id']

    # Approve the post by core_organizer
    core_organizer = f.create_user(is_core_organizer=True)
    client.login(user=core_organizer)
    url = reverse('posts-approve', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 200
    assert response.data['is_approved'] is True

    # Unapprove the post by a non_core_organizer
    client.login(user=non_core_organizer)
    url = reverse('posts-unapprove', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 403
    assert response.data['error_type'] == 'PermissionDenied'
    assert response.data['errors'][0][
        'message'] == 'You do not have permission to perform this action.'
Ejemplo n.º 5
0
def test_create_facebook_page(client):
    user = f.create_user(username='******', email='*****@*****.**', password='******')
    client.login(user=user)

    url = '/api/page'
    page = {
        "owner": user.id,
        "page_name": "test_page",
        "page_id": "123456789012345",
        "access_token": "alkKJLslfiskIEHikfOOfiffskjosflsjSSFdfsfjsbaeqoialiwqefq"
    }

    response = client.json.post(url, json.dumps(page))
    expected_keys = [
        'owner', 'page_name', 'page_id', 'access_token',
    ]

    assert response.status_code == 201
    assert set(expected_keys).issubset(response.data.keys())
    response_json = response.data

    assert response_json['owner'] == user.id
    assert response_json['page_name'] == page['page_name']
    assert response_json['page_id'] == page['page_id']
    assert response_json['access_token'] == page['access_token']
Ejemplo n.º 6
0
 def setUp(self):
     redis_connection.flushdb()
     self.variable = factories.create_variable()
     self.device = factories.create_device()
     self.variable.datasource = self.device
     self.business_account = factories.create_business_account()
     self.user = factories.create_user()
Ejemplo n.º 7
0
    def test_create_user_in_different_country(self):
        payload = {
            'english_short_name': 'Australia',
            'alpha2_code': 'AU',
            'alpha3_code': 'AUS'
        }
        where_clause = models.country.c.english_short_name == payload[
            'english_short_name']
        country_select = models.country.select(where_clause)
        self.assertIsNone(self.connection.execute(country_select).first())

        user = factories.create_user(self.connection, **payload)
        select_columns = [
            models.user.c.username,
            models.country.c.english_short_name,
            models.country.c.alpha2_code,
            models.country.c.alpha3_code,
        ]
        query = sqlalchemy.select(
            select_columns, models.user.c.username == user.username,
            models.user.join(models.country,
                             models.user.c.id == models.country.c.id))
        result = self.connection.execute(query).first()
        self.assertTrue(
            all(payload[key] == getattr(result, key)
                for key in payload.keys()))
Ejemplo n.º 8
0
def test_proposal_retraction_api(client):
    Proposal = apps.get_model('proposals.Proposal')
    proposal = f.create_proposal(status=Proposal.STATUS_CHOICES.SUBMITTED)
    url = reverse('proposal-retract', kwargs={'pk': proposal.id})
    response = client.post(url)
    assert response.status_code == status.HTTP_401_UNAUTHORIZED
    user = f.create_user(is_core_organizer=False)

    # Check that normal user cannot take this action
    client.login(user)
    response = client.post(url)
    assert response.status_code == status.HTTP_403_FORBIDDEN

    user.is_core_organizer = True
    user.save()
    user.refresh_from_db()

    # Check that core-organizer can take this action.
    client.login(user)
    response = client.post(url)
    assert response.status_code == status.HTTP_200_OK
    proposal.refresh_from_db()
    expected_keys = ('id', 'title', 'speaker', 'status', 'kind', 'level',
                     'duration', 'abstract', 'description', 'submitted_at',
                     'accepted_at', 'modified_at')
    assert set(response.data.keys()).issubset(expected_keys)
    assert proposal.status == Proposal.STATUS_CHOICES.RETRACTED
Ejemplo n.º 9
0
def test_proposal_acceptance_api(client):
    Proposal = apps.get_model('proposals.Proposal')
    proposal = f.create_proposal(status=Proposal.STATUS_CHOICES.SUBMITTED,
                                 accepted_at=None)
    url = reverse('proposal-accept', kwargs={'pk': proposal.id})
    response = client.post(url)
    assert response.status_code == status.HTTP_401_UNAUTHORIZED
    user = f.create_user(is_core_organizer=False)

    # Check that user who is not core-organizer
    # is unable to accept proposal
    client.login(user)
    response = client.post(url)
    assert response.status_code == status.HTTP_403_FORBIDDEN

    # Make user a core-organizer to accept this proposal
    user.is_core_organizer = True
    user.save()
    user.refresh_from_db()

    # Check that `accepted_at` time is still `None`
    assert proposal.accepted_at is None

    client.login(user)
    response = client.post(url)
    assert response.status_code == status.HTTP_200_OK
    proposal.refresh_from_db()
    expected_keys = ('id', 'title', 'speaker', 'status', 'kind', 'level',
                     'duration', 'abstract', 'description', 'submitted_at',
                     'accepted_at', 'modified_at')
    assert set(response.data.keys()).issubset(expected_keys)
    # Check that status is accepted and `accepted_at` now has a value.
    assert proposal.status == Proposal.STATUS_CHOICES.ACCEPTED
    assert proposal.accepted_at is not None
Ejemplo n.º 10
0
def test_psid_list_processes(mock_requests_post, client):
    fake_facebook_response_object = mock.MagicMock(**{'json.return_value': [{'code': 200}, {'code': 400}]})
    fake_facebook_response_object.__str__.return_value = '<Response [200]>'

    mock_requests_post.return_value = fake_facebook_response_object

    user = f.create_user(username='******', email='*****@*****.**', password='******')
    client.login(user=user)
    page = f.create_facebook_page(owner=user)
    label = f.create_messenger_label(owner=user, page=page)

    psid = {
        "page": page.id,
        "label": label.id,
        "psid_list": "12342342342422, 98765443210987",
    }

    url = '/api/psid'
    response = client.json.post(url, json.dumps(psid))
    expected_keys = [
        'page', 'label', 'label_associated_to', 'invalid_psid',
    ]

    assert response.status_code == 201
    assert set(expected_keys).issubset(response.data.keys())
    response_json = response.data

    assert response_json['page'] == page.id
    assert response_json['label'] == label.id
    assert response_json['label_associated_to'] == "12342342342422"
    assert response_json['invalid_psid'] == "98765443210987"
Ejemplo n.º 11
0
def test_delete_image(client):
    user = f.create_user(email='*****@*****.**', password='******')
    client.login(user=user)

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': '2018-10-09T18:30:00Z'
    }
    response = client.json.post(url, json.dumps(post))

    assert response.status_code == 201
    post_id = response.data['id']

    # Delete image before uploading
    url = reverse('posts-delete-image', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 400

    image = u.create_image(None, 'avatar.png')
    image = SimpleUploadedFile('front.png', image.getvalue())
    url = reverse('posts-upload-image', kwargs={'pk': post_id})
    response = client.post(url, {'image': image}, format='multipart')
    assert response.status_code == 200
    assert response.data["image"] is not None

    url = reverse('posts-delete-image', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 200
    assert response.data["image"] is None
Ejemplo n.º 12
0
def test_unapproving_post_throws_error_if_already_published(client):
    core_organizer = f.create_user(is_core_organizer=True)
    client.login(user=core_organizer)

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': '2018-10-09T18:30:00Z'
    }
    response = client.json.post(url, json.dumps(post))
    assert response.status_code == 201
    post_id = response.data['id']

    # Approve the post
    url = reverse('posts-approve', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 200

    Post.objects.filter(pk=post_id).update(is_posted=True)

    url = reverse('posts-unapprove', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 400
    assert response.data[
        'error_message'] == 'Can not unapprove, post has already been published'
Ejemplo n.º 13
0
 def setUp(self):
     self.user = f.create_user(username='******',
                               email='*****@*****.**',
                               password='******')
     self.page = f.create_facebook_page(owner=self.user,
                                        page_name='test_page')
     self.label = f.create_messenger_label(owner=self.user,
                                           page=self.page,
                                           label_name='test_label')
Ejemplo n.º 14
0
 def test_update_duplicate_values_for_unique_fields_forbidden(self):
     test_fields = ('username', 'email')
     user = factories.create_user(self.connection)
     for test_field in test_fields:
         payload = {test_field: getattr(self.user, test_field)}
         update_duplicate_query = models.user.update(
             models.user.c.id == user.id, payload)
         self.assertRaises(exc.IntegrityError, self.connection.execute,
                           update_duplicate_query)
Ejemplo n.º 15
0
    def test_put(self):
        user = create_user(username='******')
        create_doctor(user=user)
        post_data = dict(
            username='******',
            first_name='the doctor',
            city='city',
            address='address',
            phone_number='123456',
        )

        response = self.client.put(self.url, data=post_data, format='json')
        self.assertEqual(response.status_code, 403)

        self.client.login(username='******', password='******')
        response = self.client.put(self.url, data=post_data, format='json')
        self.assertEqual(response.status_code, 200)
Ejemplo n.º 16
0
def test_unapproving_post_throws_error_if_already_unapproved(client):
    core_organizer = f.create_user(is_core_organizer=True)
    client.login(user=core_organizer)

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': '2018-10-09T18:30:00Z'
    }
    response = client.json.post(url, json.dumps(post))
    assert response.status_code == 201
    post_id = response.data['id']

    # Unapprove the post without approving
    url = reverse('posts-unapprove', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 400
    assert response.data['error_message'] == 'Post has not been approved yet'
Ejemplo n.º 17
0
def test_exceeding_batch_limit(mock_FB_BATCH_REQUEST, client):
    user = f.create_user(username='******', email='*****@*****.**', password='******')
    client.login(user=user)
    page = f.create_facebook_page(owner=user)
    label = f.create_messenger_label(owner=user, page=page)

    psid = {
        "page": page.id,
        "label": label.id,
        "psid_list": "12342342342422, 98765443210987",
    }

    url = '/api/psid'

    mock_FB_BATCH_REQUEST.__getitem__.return_value = 1
    with pytest.raises(Exception) as exc:
        client.json.post(url, json.dumps(psid))

    assert exc.value.args[0] == "Facebook batch request limit exceeded. Allowed 1, received 2"
Ejemplo n.º 18
0
def test_proposal_listing_api(client):

    no_of_test_proposals = 6
    data = {
        'page': 2,
        'per_page': 2,
    }
    f.create_proposal(n=no_of_test_proposals)
    url = reverse('proposal-list')
    user = f.create_user()

    client.login(user)
    response = client.get(url, data=data)
    assert response.status_code == status.HTTP_200_OK
    assert response.data['previous'] is not None
    assert response.data['next'] is not None
    assert response.data['count'] == no_of_test_proposals
    assert response.data['results'] is not None
    assert response.data['results'][0]['submitted_at'] > response.data[
        'results'][1]['submitted_at']
Ejemplo n.º 19
0
def test_create_messenger_label(mock_requests_post, client):

    # Test successful creation
    fake_facebook_response_object = mock.MagicMock(**{'json.return_value': {'id': '1234567890'}})
    fake_facebook_response_object.__str__.return_value = '<Response [200]>'

    mock_requests_post.return_value = fake_facebook_response_object

    user = f.create_user(username='******', email='*****@*****.**', password='******')
    client.login(user=user)
    page = f.create_facebook_page()

    url = '/api/label'
    label = {
        "owner": user.id,
        "page": page.id,
        "label_name": "test_label"
    }

    response = client.json.post(url, json.dumps(label))
    expected_keys = [
        'owner', 'page', 'label_name', 'label_id'
    ]

    assert response.status_code == 201
    assert set(expected_keys).issubset(response.data.keys())
    mock_requests_post.assert_called_once()
    response_json = response.data

    assert response_json['owner'] == user.id
    assert response_json['page'] == page.id
    assert response_json['label_name'] == label['label_name']
    assert response_json['label_id'] == '1234567890'

    # Test unsuccessful label creation
    fake_facebook_response_object.__str__.return_value = '<Response [400]>'

    with pytest.raises(Exception) as exc:
        client.json.post(url, json.dumps(label))

    assert exc.value.args[0] == 'Invalid access token or label name!'
Ejemplo n.º 20
0
def test_create_text_post(client):
    user = f.create_user(email='*****@*****.**', password='******')
    client.login(user=user)
    scheduled_time = '2018-10-09T18:30:00Z'

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': scheduled_time
    }
    response = client.json.post(url, json.dumps(post))
    expected_keys = [
        'is_posted', 'created_at', 'scheduled_time', 'id', 'posted_time',
        'text', 'is_approved', 'image', 'posted_at', 'modified_at',
        'posted_by', 'approval_time'
    ]
    assert set(expected_keys).issubset(response.data.keys())
    response_json = response.data
    assert response_json['posted_by'] == user.email
    assert response_json['text'] == post['text']
    assert response_json['posted_at'] == post['posted_at']
    assert response_json['scheduled_time'] == scheduled_time
Ejemplo n.º 21
0
def test_unapproving_post_allowed_to_core_organizer(client):
    core_organizer = f.create_user(is_core_organizer=True)
    client.login(user=core_organizer)

    url = reverse('posts-list')
    post = {
        'text': 'Announcement!',
        'posted_at': 'fb',
        'scheduled_time': '2018-10-09T18:30:00Z'
    }
    response = client.json.post(url, json.dumps(post))
    assert response.status_code == 201
    post_id = response.data['id']

    # Approve the post
    url = reverse('posts-approve', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 200

    # Unapprove the post
    url = reverse('posts-unapprove', kwargs={'pk': post_id})
    response = client.post(url)
    assert response.status_code == 200
Ejemplo n.º 22
0
def test_proposal_creation_api(client):
    Proposal = apps.get_model('proposals.Proposal')
    url = reverse('proposal-list')
    user = f.create_user()
    data = {
        'title': 'Test Proposal',
        'kind': 'talk',
        'level': 'beginner',
        'duration': '00:30:00',
        'abstract': 'Proposal abstract',
        'description': 'Proposal description',
    }
    client.login(user)

    f.create_proposal_kind(kind='talk')
    response = client.post(url, data=data)
    assert response.status_code == status.HTTP_201_CREATED
    assert response.data["speaker"] == user.email
    assert response.data["submitted_at"] is not None
    assert response.data["modified_at"] is not None
    assert response.data["status"] == Proposal.STATUS_CHOICES.SUBMITTED

    proposal = Proposal.objects.get(id=response.data["id"])
    assert proposal.title == response.data["title"]
Ejemplo n.º 23
0
 def setUp(self):
     self.url = reverse('api:category-import-csv')
     create_user()
Ejemplo n.º 24
0
 def setUp(self):
     category = create_category()
     self.url = reverse('api:category-detail',
                        kwargs=dict(pk=str(category.id)))
     create_user()
Ejemplo n.º 25
0
 def setUp(self):
     self.url = reverse('api:doctor')
     create_user()
Ejemplo n.º 26
0
 def setUp(self):
     self.url = reverse('api:category')
     create_user()
Ejemplo n.º 27
0
def create_user(password):
    user = f.create_user()
    user.set_password(password)
    user.save()
    return user
Ejemplo n.º 28
0
    def setUp(self):
        doctor = create_doctor()
        self.url = reverse('api:doctor-detail', kwargs=dict(pk=str(doctor.id)))
        create_user()

        self.doctor = doctor
Ejemplo n.º 29
0
 def setUp(self):
     product = create_product()
     self.url = reverse('api:product-by-name',
                        kwargs=dict(name=product.name))
     create_user()
Ejemplo n.º 30
0
 def setUp(self):
     self.url = reverse('api:product')
     create_user()
Ejemplo n.º 31
0
 def setUp(self):
     product = create_product()
     self.url = reverse('api:product-detail',
                        kwargs=dict(pk=str(product.id)))
     create_user()