Example #1
0
def test_create_me_article(client):
    new_article = {
        'slug': fake.slug(),
        'title': fake.text(250),
        'content': fake.text(500),
        'category_id': fake.random_int(1, 3),
        'status': 'DRAFT'
    }
    user_payload = {
        'userid': fake.random_int(2, 100),
        'username': fake.user_name(),
        'email': fake.email()
    }

    header = token_validation.generate_token_header(user_payload, PRIVATE_KEY)
    headers = {
        'Authorization': header,
    }
    response = client.post('/api/me/articles',
                           data=new_article,
                           headers=headers)
    result = response.json
    assert http.client.CREATED == response.status_code

    expected = {
        'id': ANY,
        'slug': new_article['slug'],
        'title': new_article['title'],
        'content': new_article['content'],
        'author_id': ANY,
        'category_id': new_article['category_id'],
        'status': new_article['status'],
        'published': ANY
    }
    assert result == expected
Example #2
0
def article_fixture(client):
    '''
    Generate five articles in the system.
    '''
    article_ids = []

    user_payload = {
        'userid': 1,
        'username': '******',
        'email': '*****@*****.**'
    }

    for _ in range(5):
        article = {
            'slug': fake.slug(),
            'title': fake.text(250),
            'content': fake.text(500),
            'category_id': fake.random_int(1, 3),
            'status': 'DRAFT'
        }
        header = token_validation.generate_token_header(
            user_payload, PRIVATE_KEY)
        headers = {
            'Authorization': header,
        }
        response = client.post('/api/me/articles',
                               data=article,
                               headers=headers)
        assert http.client.CREATED == response.status_code
        result = response.json
        article_ids.append(result['id'])

    yield article_ids
def test_valid_token_header():
    payload = {
        'username': '******',
        'email': '*****@*****.**'
    }
    header = token_validation.generate_token_header(payload, PRIVATE_KEY)
    result = token_validation.validate_token_header(header, PUBLIC_KEY)
    assert 'whiteboardwarrior' == result['username']
def test_valid_token_header_invalid_key():
    payload = {
        'username': '******',
        'email': '*****@*****.**'
    }
    header = token_validation.generate_token_header(payload, PRIVATE_KEY)
    result = token_validation.validate_token_header(header, INVALID_PUBLIC_KEY)
    assert None is result
Example #5
0
def test_get_me_articles(client, article_fixture):
    user_payload = {
        'userid': 1,
        'username': '******',
        'email': '*****@*****.**'
    }

    header = token_validation.generate_token_header(user_payload, PRIVATE_KEY)
    headers = {
        'Authorization': header,
    }

    response = client.get('/api/me/articles', headers=headers)
    assert http.client.OK == response.status_code
Example #6
0
def test_get_me_articles_not_found(client):
    user_payload = {
        'userid': fake.random_int(2, 100),
        'username': fake.user_name(),
        'email': fake.email()
    }

    header = token_validation.generate_token_header(user_payload, PRIVATE_KEY)
    headers = {
        'Authorization': header,
    }

    response = client.get('/api/me/articles/', headers=headers)
    assert http.client.NOT_FOUND == response.status_code
Example #7
0
def test_delete_me_article_invalid_articleid(client, article_fixture):
    article_id = 1234455
    user_payload = {
        'userid': 2,
        'username': '******',
        'email': '*****@*****.**'
    }

    header = token_validation.generate_token_header(user_payload, PRIVATE_KEY)
    headers = {
        'Authorization': header,
    }

    url = f'/api/me/article/{article_id}'
    response = client.delete(url, headers=headers)
    assert http.client.NOT_FOUND == response.status_code