Beispiel #1
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"
Beispiel #2
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')
Beispiel #3
0
 def test_create_facebook_page(self):
     page = {
         "owner":
         self.user,
         "page_name":
         "test_page",
         "page_id":
         "123456789012345",
         "access_token":
         "alkKJLslfiskIEHikfOOfiffskjosflsjSSFdfsfjsbaeqoialiwqefq"
     }
     page = f.create_facebook_page(**page)
     assert page.id
     assert page.owner.username == 'test_user'
     assert page.page_name == "test_page"
     assert page.page_id == "123456789012345"
     assert page.access_token == "alkKJLslfiskIEHikfOOfiffskjosflsjSSFdfsfjsbaeqoialiwqefq"
     assert str(page) == str(page.page_name)
Beispiel #4
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"
Beispiel #5
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!'