def tweet_fixture(client): ''' Generate three tweets in the system. ''' tweet_ids = [] for _ in range(3): tweet = { 'text': fake.text(240), } header = token_validation.generate_token_header( fake.name(), PRIVATE_KEY) headers = { 'Authorization': header, } response = client.post('/api/me/tweets/', data=tweet, headers=headers) assert http.client.CREATED == response.status_code result = response.json tweet_ids.append(result['id']) yield tweet_ids # Clean up all tweets response = client.get('/api/tweets/') tweets = response.json for tweet in tweets: tweet_id = tweet['id'] url = f'/admin/tweets/{tweet_id}/' response = client.delete(url) assert http.client.NO_CONTENT == response.status_code
def test_list_me_tweets(client, tweet_fixture): username = fake.name() text = fake.text(240) # Create a new tweet new_tweet = { 'text': text, } header = token_validation.generate_token_header(username, PRIVATE_KEY) headers = { 'Authorization': header, } response = client.post('/api/me/tweets/', data=new_tweet, headers=headers) result = response.json assert http.client.CREATED == response.status_code # Get the tweets of the user response = client.get('/api/me/tweets/', headers=headers) results = response.json assert http.client.OK == response.status_code assert len(results) == 1 result = results[0] expected_result = { 'id': ANY, 'username': username, 'text': text, 'timestamp': ANY, } assert result == expected_result
def test_list_tweets_search(client, tweet_fixture): username = fake.name() new_tweet = { 'username': username, 'text': 'Do you dream about the City at the End of Time?' } header = token_validation.generate_token_header(username, PRIVATE_KEY) headers = { 'Authorization': header, } response = client.post('/api/me/tweets/', data=new_tweet, headers=headers) assert http.client.CREATED == response.status_code response = client.get('/api/tweets/?search=City') result = response.json assert http.client.OK == response.status_code assert len(result) > 0 # Check that the returned values contain "city" for tweet in result: expected = { 'text': ANY, 'username': username, 'id': ANY, 'timestamp': ANY, } assert expected == tweet assert 'city' in tweet['text'].lower()
def test_create_me_tweet(client): new_tweet = { 'username': fake.name(), 'text': fake.text(240), } header = token_validation.generate_token_header(fake.name(), PRIVATE_KEY) headers = { 'Authorization': header, } response = client.post('/api/me/tweets/', data=new_tweet, headers=headers) result = response.json assert http.client.CREATED == response.status_code expected = { 'id': ANY, 'username': ANY, 'text': new_tweet['text'], 'timestamp': '2019-05-07T13:47:34', } assert result == expected
def test_valid_token_header(): header = token_validation.generate_token_header('alice', PRIVATE_KEY) result = token_validation.validate_token_header(header, PUBLIC_KEY) assert 'alice' == result
def test_valid_token_header_invalid_key(): header = token_validation.generate_token_header('alice', PRIVATE_KEY) result = token_validation.validate_token_header(header, INVALID_PUBLIC_KEY) assert None is result