The first thing we're going to do is create a twitter application, generate your twitter keys, and then create some quick and dirty tests to verify your twitter access.
Twitter's authentication is a little complex. However I found a wrapper for httplib that will handle the oauth authentication, while still valid for our example. So we move forward!
- Install the oauth2 library. apparently the oauth2 module I'm using has a bug. Basically GET, and DELETE commands will fail! Fun. I don't know of any other library that does what this one does, and I figured out a fix, so we'll install it from my fixed fork of their repo (I will soon post a pull request to their repo to merge the bug fix back in, but the repo hasn't been updated since 2015. I think it's abandoned).
- git clone https://github.com/lightmanca/python-oauth2.git
- or download the zip file, and extract.
- cd python-oauth2
- python python setup.py install
- Install pytest
- pip install pytest
- Go to http://apps.twitter.com, and create an application.
- Once your application is created, generate a consumer token. Copy your access token information, and consumer token information to a test editor for now.
- ***NOTE*** Never ever check these keys into your git repo. We'll go over ways to secure access keys, and user information at a later time.
- Lets create a python project in intellij, or your favorite IDE. This will be used throughout the tutorial.
- create a test file. test files need to start with "test" in order to be discovered by pytest. Here is what I've got so far:
import random
import string
import urllib
from urllib.parse import urlencode
import oauth2
class TestTwitterCRUD:
CONSUMER_KEY = "**Your consumer Key**"
CONSUMER_SECRET = "**Your consumer Secret**"
ACCESS_TOKEN = "**Your access token**"
ACCESS_TOKEN_SECRET = "***Your access token secret**"
def test_get_timeline(self):
home_timeline = self.oauth_req('https://api.twitter.com/1.1/statuses/home_timeline.json')
print(home_timeline)
assert home_timeline is not None
def test_post_timeline(self):
status = "Test Status {}".format(self.make_random_string(6))
payload = "status={}".format(urllib.parse.quote(status))
response = self.oauth_req('https://api.twitter.com/1.1/statuses/update.json', http_method="POST", post_body=payload)
print(response)
assert response is not None
assert status in str(response)
def oauth_req(self, url, http_method="GET", post_body="", http_headers=None):
consumer = oauth2.Consumer(key=self.CONSUMER_KEY, secret=self.CONSUMER_SECRET)
token = oauth2.Token(key=self.ACCESS_TOKEN, secret=self.ACCESS_TOKEN_SECRET)
client = oauth2.Client(consumer, token)
resp, content = client.request( url, method=http_method, body=post_body, headers=http_headers )
return content
def make_random_string(self, num_chars):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(num_chars))
As you have noticed, there is no test framework, yet. We're just making web api calls to twitter, and verifying that we kinda get something back. We also are not verifying return codes. We'll start making our test framework in the next sections.
It's interesting though that even though we don't have any test framework at all you can pretty clearly see what is going on here. However if we added 30 more tests it would be harder maintain this code.
But first, since I almost checked in all my twitter secret keys we need to visit creating a config file, which will be the next post.
This file is committed to branch https://github.com/lightmanca/TheTestFrameworkBlog/tree/twitter_no_framework_tests
No comments:
Post a Comment