Skip to content

Commit

Permalink
Add betamax support for OAuth2Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bboe committed Apr 11, 2015
1 parent 2d3d7d1 commit a462d02
Show file tree
Hide file tree
Showing 23 changed files with 46 additions and 21 deletions.
45 changes: 24 additions & 21 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

with Betamax.configure() as config:
config.cassette_library_dir = 'tests/fixtures/cassettes'
config.default_cassette_options['match_requests_on'].append('body')


def flair_diff(root, other):
Expand Down Expand Up @@ -1459,6 +1460,7 @@ def test_get_access_information(self):
self.assertEqual(expected, token)
self.assertNotEqual(None, self.r.user)

@betamax
def test_get_access_information_with_invalid_code(self):
self.assertRaises(errors.OAuthInvalidGrant,
self.r.get_access_information, 'invalid_code')
Expand All @@ -1475,57 +1477,58 @@ def test_invalid_app_authorize_url(self):
self.assertRaises(errors.OAuthAppRequired,
self.r.get_authorize_url, 'dummy_state')

@betamax
def test_invalid_set_access_credentials(self):
self.assertRaises(errors.OAuthInvalidToken,
self.r.set_access_credentials,
set(('identity',)), 'dummy_access_token')

@reddit_only
@betamax
def test_scope_edit(self):
self.r.refresh_access_information(self.refresh_token['edit'])
submission = Submission.from_id(self.r, self.submission_edit_id)
self.assertEqual(submission, submission.edit('Edited text'))

@reddit_only
@betamax
def test_scope_history(self):
self.r.refresh_access_information(self.refresh_token['history'])
self.assertTrue(list(self.r.get_redditor(self.un).get_liked()))

@reddit_only
@betamax
def test_scope_identity(self):
self.r.refresh_access_information(self.refresh_token['identity'])
self.assertEqual(self.un, self.r.get_me().name)

@reddit_only
@betamax
def test_scope_modconfig(self):
self.r.refresh_access_information(self.refresh_token['modconfig'])
self.r.get_subreddit(self.sr).set_settings('foobar')
retval = self.r.get_subreddit(self.sr).get_stylesheet()
self.assertTrue('images' in retval)

@reddit_only
@betamax
def test_scope_modflair(self):
self.r.refresh_access_information(self.refresh_token['modflair'])
self.r.get_subreddit(self.sr).set_flair(self.un, 'foobar')

@reddit_only
@betamax
def test_scope_modlog(self):
num = 50
self.r.refresh_access_information(self.refresh_token['modlog'])
result = self.r.get_subreddit(self.sr).get_mod_log(limit=num)
self.assertEqual(num, len(list(result)))

@reddit_only
@betamax
def test_scope_modposts(self):
self.r.refresh_access_information(self.refresh_token['modposts'])
Submission.from_id(self.r, self.submission_edit_id).remove()

@reddit_only
@betamax
def test_scope_mysubreddits(self):
self.r.refresh_access_information(self.refresh_token['mysubreddits'])
self.assertTrue(list(self.r.get_my_moderation()))

@reddit_only
@betamax
def test_scope_creddits(self):
# Assume there are insufficient creddits.
self.r.refresh_access_information(
Expand All @@ -1543,13 +1546,13 @@ def test_scope_creddits(self):
self.assertRaises(errors.InsufficientCreddits, sub.gild)
self.assertRaises(errors.InsufficientCreddits, sub.comments[0].gild)

@reddit_only
@betamax
def test_scope_privatemessages(self):
self.r.refresh_access_information(
self.refresh_token['privatemessages'])
self.assertTrue(list(self.r.get_inbox()))

@reddit_only
@betamax
def test_scope_read(self):
self.r.refresh_access_information(self.refresh_token['read'])
self.assertTrue(self.r.get_subreddit(self.priv_sr).subscribers > 0)
Expand All @@ -1559,57 +1562,57 @@ def test_scope_read(self):
method2 = self.r.get_submission(submission_id=self.priv_submission_id)
self.assertEqual(method1, method2)

@reddit_only
@betamax
def test_scope_read_get_front_page(self):
self.r.refresh_access_information(self.refresh_token['mysubreddits'])
subscribed = list(self.r.get_my_subreddits(limit=None))
self.r.refresh_access_information(self.refresh_token['read'])
for post in self.r.get_front_page():
self.assertTrue(post.subreddit in subscribed)

@reddit_only
@betamax
def test_scope_read_get_sub_listingr(self):
self.r.refresh_access_information(self.refresh_token['read'])
subreddit = self.r.get_subreddit(self.priv_sr)
self.assertTrue(list(subreddit.get_top()))

@reddit_only
@betamax
def test_scope_read_get_submission_by_url(self):
url = ("http://www.reddit.com/r/reddit_api_test_priv/comments/16kbb7/"
"google/")
self.r.refresh_access_information(self.refresh_token['read'])
submission = Submission.from_url(self.r, url)
self.assertTrue(submission.num_comments != 0)

@reddit_only
@betamax
def test_scope_read_priv_sr_comments(self):
self.r.refresh_access_information(self.refresh_token['read'])
self.assertTrue(list(self.r.get_comments(self.priv_sr)))

@reddit_only
@betamax
def test_scope_read_priv_sub_comments(self):
self.r.refresh_access_information(self.refresh_token['read'])
submission = Submission.from_id(self.r, self.priv_submission_id)
self.assertTrue(submission.comments)

@reddit_only
@betamax
def test_scope_submit(self):
self.r.refresh_access_information(self.refresh_token['submit'])
result = self.r.submit(self.sr, 'OAuth Submit', text='Foo')
self.assertTrue(isinstance(result, Submission))

@reddit_only
@betamax
def test_scope_subscribe(self):
self.r.refresh_access_information(self.refresh_token['subscribe'])
self.r.get_subreddit(self.sr).subscribe()

@reddit_only
@betamax
def test_scope_vote(self):
self.r.refresh_access_information(self.refresh_token['vote'])
submission = Submission.from_id(self.r, self.submission_edit_id)
submission.clear_vote()

@reddit_only
@betamax
def test_set_access_credentials(self):
self.assertTrue(self.r.user is None)
result = self.r.refresh_access_information(
Expand All @@ -1618,7 +1621,7 @@ def test_set_access_credentials(self):
self.r.set_access_credentials(**result)
self.assertFalse(self.r.user is None)

@reddit_only
@betamax
def test_oauth_without_identy_doesnt_set_user(self):
self.assertTrue(self.r.user is None)
self.r.refresh_access_information(self.refresh_token['edit'])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http_interactions": [{"request": {"body": {"string": "redirect_uri=https%3A%2F%2F127.0.0.1%3A65010%2Fauthorize_callback&code=invalid_code&grant_type=authorization_code", "encoding": "utf-8"}, "headers": {"Content-Length": ["113"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "User-Agent": ["PRAW_test_suite PRAW/2.1.21 Python/2.7.5 Darwin-13.4.0-x86_64-i386-64bit"], "Connection": ["keep-alive"], "Content-Type": ["application/x-www-form-urlencoded"], "Authorization": ["Basic c3RKbFVTVWJQUWU1bFE6aVUtTHNPenlKSDdCRFZvcS1xT1dORXEyenVJ"]}, "method": "POST", "uri": "https://ssl.reddit.com/api/v1/access_token/"}, "response": {"body": {"string": "{\"error\": \"invalid_grant\"}", "encoding": "UTF-8"}, "headers": {"content-length": ["26"], "x-xss-protection": ["1; mode=block"], "x-content-type-options": ["nosniff"], "set-cookie": ["__cfduid=d537c4d7cb46f1cf1de5bfef92e2f7b051428737839; expires=Sun, 10-Apr-16 07:37:19 GMT; path=/; domain=.reddit.com; HttpOnly"], "vary": ["Accept-Encoding"], "server": ["cloudflare-nginx"], "connection": ["keep-alive"], "cache-control": ["no-cache"], "date": ["Sat, 11 Apr 2015 07:37:20 GMT"], "x-frame-options": ["SAMEORIGIN"], "x-moose": ["majestic"], "content-type": ["application/json; charset=UTF-8"], "cf-ray": ["1d55068a542b0651-SJC"]}, "status": {"message": "OK", "code": 200}, "url": "https://ssl.reddit.com/api/v1/access_token/"}, "recorded_at": "2015-04-11T07:37:30"}], "recorded_with": "betamax/0.4.1"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Accept": ["*/*"], "Connection": ["keep-alive"], "Accept-Encoding": ["gzip, deflate"], "Authorization": ["bearer dummy_access_token"], "User-Agent": ["PRAW_test_suite PRAW/2.1.21 Python/2.7.5 Darwin-13.4.0-x86_64-i386-64bit"]}, "method": "GET", "uri": "https://oauth.reddit.com/api/v1/me.json"}, "response": {"body": {"string": "{\"error\": 401}", "encoding": "UTF-8"}, "headers": {"content-length": ["14"], "x-xss-protection": ["1; mode=block"], "x-reddit-tracking": ["https://pixel.redditmedia.com/pixel/of_destiny.png?v=S6xb7zlYHQbrG2NKJVD5hnxmKae%2F%2BxH%2Bqn1dYu7DjKYdgAewxQW92e6EA9QOiM%2FTk6%2Bli6PcJOQ%3D"], "x-content-type-options": ["nosniff"], "access-control-expose-headers": ["X-Reddit-Tracking, X-Moose"], "set-cookie": ["__cfduid=d638df043cba862b2f61081f00642930b1428737934; expires=Sun, 10-Apr-16 07:38:54 GMT; path=/; domain=.reddit.com; HttpOnly"], "www-authenticate": ["Bearer realm=\"reddit\", error=\"invalid_token\""], "server": ["cloudflare-nginx"], "connection": ["keep-alive"], "x-ua-compatible": ["IE=edge"], "cache-control": ["no-cache"], "date": ["Sat, 11 Apr 2015 07:38:54 GMT"], "x-frame-options": ["SAMEORIGIN"], "access-control-allow-origin": ["*"], "x-moose": ["majestic"], "content-type": ["application/json; charset=UTF-8"], "cf-ray": ["1d5508db56300da3-SJC"]}, "status": {"message": "Unauthorized", "code": 401}, "url": "https://oauth.reddit.com/api/v1/me.json"}, "recorded_at": "2015-04-11T07:39:05"}], "recorded_with": "betamax/0.4.1"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http_interactions": [{"request": {"body": {"string": "grant_type=refresh_token&refresh_token=FFx_0G7Zumyh4AWzIo39bG9KdIM&redirect_uri=https%3A%2F%2F127.0.0.1%3A65010%2Fauthorize_callback", "encoding": "utf-8"}, "headers": {"Content-Length": ["132"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "User-Agent": ["PRAW_test_suite PRAW/2.1.21 Python/2.7.5 Darwin-13.4.0-x86_64-i386-64bit"], "Connection": ["keep-alive"], "Content-Type": ["application/x-www-form-urlencoded"], "Authorization": ["Basic c3RKbFVTVWJQUWU1bFE6aVUtTHNPenlKSDdCRFZvcS1xT1dORXEyenVJ"]}, "method": "POST", "uri": "https://ssl.reddit.com/api/v1/access_token/"}, "response": {"body": {"base64_string": "H4sIAAAAAAAAA6tWSkxOTi0uji/Jz07NU7JSUDI3NjCyMDPXLauMDDT3yTWuyq2MqAp2tgg3yzCNiHBN9MqLVNJRUAKrjy+pLEgFaUpKTSxKLQKJp1YUZBalFsdnggwzNjMw0FFQKk7OhyhLTcksUaoFAJ/oLAt0AAAA", "encoding": "UTF-8"}, "headers": {"x-xss-protection": ["1; mode=block"], "x-content-type-options": ["nosniff"], "content-encoding": ["gzip"], "transfer-encoding": ["chunked"], "set-cookie": ["__cfduid=de18fffd5cf3ff8305436d90d608a4f781428738079; expires=Sun, 10-Apr-16 07:41:19 GMT; path=/; domain=.reddit.com; HttpOnly"], "vary": ["Accept-Encoding"], "server": ["cloudflare-nginx"], "connection": ["keep-alive"], "cache-control": ["no-cache"], "date": ["Sat, 11 Apr 2015 07:41:19 GMT"], "x-frame-options": ["SAMEORIGIN"], "x-moose": ["majestic"], "content-type": ["application/json; charset=UTF-8"], "cf-ray": ["1d550c6289681219-SJC"]}, "status": {"message": "OK", "code": 200}, "url": "https://ssl.reddit.com/api/v1/access_token/"}, "recorded_at": "2015-04-11T07:41:29"}], "recorded_with": "betamax/0.4.1"}
1 change: 1 addition & 0 deletions tests/fixtures/cassettes/test_scope_creddits.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/fixtures/cassettes/test_scope_edit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http_interactions": [{"request": {"body": {"string": "grant_type=refresh_token&refresh_token=FFx_0G7Zumyh4AWzIo39bG9KdIM&redirect_uri=https%3A%2F%2F127.0.0.1%3A65010%2Fauthorize_callback", "encoding": "utf-8"}, "headers": {"Content-Length": ["132"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "User-Agent": ["PRAW_test_suite PRAW/2.1.21 Python/2.7.5 Darwin-13.4.0-x86_64-i386-64bit"], "Connection": ["keep-alive"], "Content-Type": ["application/x-www-form-urlencoded"], "Authorization": ["Basic c3RKbFVTVWJQUWU1bFE6aVUtTHNPenlKSDdCRFZvcS1xT1dORXEyenVJ"]}, "method": "POST", "uri": "https://ssl.reddit.com/api/v1/access_token/"}, "response": {"body": {"base64_string": "H4sIAAAAAAAAA6tWSkxOTi0uji/Jz07NU7JSUDI3NjCyMDPXNU9zdS6pDCh1Ti4t9jI0yk+NsghISywz8i+1UNJRUAKrjy+pLEgFaUpKTSxKLQKJp1YUZBalFsdnggwzNjMw0FFQKk7OhyhLTcksUaoFAE8drFZ0AAAA", "encoding": "UTF-8"}, "headers": {"x-xss-protection": ["1; mode=block"], "x-content-type-options": ["nosniff"], "content-encoding": ["gzip"], "transfer-encoding": ["chunked"], "set-cookie": ["__cfduid=de7a8082d1b018149be421b5c079691ae1428737957; expires=Sun, 10-Apr-16 07:39:17 GMT; path=/; domain=.reddit.com; HttpOnly"], "vary": ["Accept-Encoding"], "server": ["cloudflare-nginx"], "connection": ["keep-alive"], "cache-control": ["no-cache"], "date": ["Sat, 11 Apr 2015 07:39:18 GMT"], "x-frame-options": ["SAMEORIGIN"], "x-moose": ["majestic"], "content-type": ["application/json; charset=UTF-8"], "cf-ray": ["1d55096d18770d73-SJC"]}, "status": {"message": "OK", "code": 200}, "url": "https://ssl.reddit.com/api/v1/access_token/"}, "recorded_at": "2015-04-11T07:39:28"}, {"request": {"body": {"string": "text=Edited+text&thing_id=t3_16i92b&api_type=json", "encoding": "utf-8"}, "headers": {"Content-Length": ["49"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "User-Agent": ["PRAW_test_suite PRAW/2.1.21 Python/2.7.5 Darwin-13.4.0-x86_64-i386-64bit"], "Connection": ["keep-alive"], "Content-Type": ["application/x-www-form-urlencoded"], "Authorization": ["bearer 7302867-7fECtyPuCcusJ12oeZ8Pfav2Ou8"]}, "method": "POST", "uri": "https://oauth.reddit.com/api/editusertext/.json"}, "response": {"body": {"base64_string": "H4sIAAAAAAAAA2yRy07DMBBFf8V40VXKo0hItGKBEJXYQCVgRZHlxNNmiB/R2ImoIv4djWlKF92N71yfOx4P8isGL+dikEAUKMq5+PgshDQ66SynGv02y4Ns0Bs5FzJdy2OLCU4jQ2QEuzknMAaT0i2qBDGxtdTeg1Hljk2r3f3q6Q1ieo9AM247MKgVuBIYP/wUQsau/OPwjRNETkrwnVSdnGXPxKbF2XQqXh/Uy3IpptPJNi1YNNiLyuoY79bSmbUc9ZaLR4MJjGASaxdZXPtcG+xH7wh+3nOPJ+DwIwy3LDbAK/OdtYWQXQRSBG2gdNhvhKojUPnl/06LvlEbq5HUHr1vYN771Q3ezkpO2KI1eVmXhZCaqhr7fEzUQSFkZbFqsrDRNrLyF68IdAz+MIXuUh3o9Kf4zqkqOAc+T81BsQoEY2jbUujB/AIAAP//nJNBboMwEEWvEuUAcYCSkOxygi66riwDbhm1jpE9DivfvZoxFoSqm+6Qx2j+f/87hTrLtA/tZNGs9g7Q95q6kQ9wCKa9K+DQmGNOWiaTWMsSa3uimWau++uueCmbc3W+1JcDLV9x6ryXHO+iIrn6e97biRGwpVUOmwaqhDQL9wjdFzwdgZfUgoX7qJ1RJI6MCCc2xRWZp0hJCktSpQ+tARQMXRkCvMdKLmF3TqsZQ1U3xbE5nkrGEBxTHBDHqxDTNM1v79BZ85/tT+Bytz+sbZWjMQJ+s7rXW8Bh98Y/rgTKgN1vkT14hPtnAD+whzmETfON7bdP5AF+Tj/zpkoul5jAyF8xvscYfwAAAP//AwAbfw38zQQAAA==", "encoding": "UTF-8"}, "headers": {"x-ratelimit-remaining": ["514.0"], "x-xss-protection": ["1; mode=block"], "x-content-type-options": ["nosniff"], "content-encoding": ["gzip"], "transfer-encoding": ["chunked"], "set-cookie": ["__cfduid=d89970b14a22ab08edf4847f87ee2c3681428737958; expires=Sun, 10-Apr-16 07:39:18 GMT; path=/; domain=.reddit.com; HttpOnly"], "vary": ["accept-encoding"], "server": ["cloudflare-nginx"], "connection": ["keep-alive"], "x-ua-compatible": ["IE=edge"], "pragma": ["no-cache"], "cache-control": ["no-cache, no-cache"], "date": ["Sat, 11 Apr 2015 07:39:19 GMT"], "x-frame-options": ["SAMEORIGIN"], "x-moose": ["majestic"], "content-type": ["application/json; charset=UTF-8"], "cf-ray": ["1d550971e904012c-SJC"], "x-ratelimit-used": ["86"], "x-ratelimit-reset": ["41"]}, "status": {"message": "OK", "code": 200}, "url": "https://oauth.reddit.com/api/editusertext/.json"}, "recorded_at": "2015-04-11T07:39:29"}], "recorded_with": "betamax/0.4.1"}

0 comments on commit a462d02

Please sign in to comment.