| |
@@ -4,6 +4,7 @@
|
| |
from flask import json
|
| |
from os.path import dirname
|
| |
import vcr
|
| |
+ from mock import mock
|
| |
from werkzeug.datastructures import ImmutableMultiDict
|
| |
|
| |
import hubs
|
| |
@@ -357,6 +358,117 @@
|
| |
result = self.app.get(url)
|
| |
self.assertEqual(result.status_code, 404)
|
| |
|
| |
+ def mocked_requests_get(*args, **kwargs):
|
| |
+ class MockResponse:
|
| |
+ def __init__(self, json_data, status_code):
|
| |
+ self.json_data = json_data
|
| |
+ self.status_code = status_code
|
| |
+
|
| |
+ def json(self):
|
| |
+ return self.json_data
|
| |
+
|
| |
+ if '/plus_plus/decause/status' in args:
|
| |
+ data = {
|
| |
+ "current": 0,
|
| |
+ "decrements": 0,
|
| |
+ "increments": 0,
|
| |
+ "release": "f24",
|
| |
+ "total": 0,
|
| |
+ "username": "decause"
|
| |
+ }
|
| |
+ return MockResponse(json_data=data, status_code=200)
|
| |
+
|
| |
+ return MockResponse({}, 404)
|
| |
+
|
| |
+ def mocked_requests_post(*args, **kwargs):
|
| |
+ class MockResponse:
|
| |
+ def __init__(self, json_data, status_code):
|
| |
+ self.json_data = json_data
|
| |
+ self.status_code = status_code
|
| |
+
|
| |
+ def json(self):
|
| |
+ return self.json_data
|
| |
+
|
| |
+ if '/plus_plus/decause/update' in args:
|
| |
+ data = {
|
| |
+ "current": 1,
|
| |
+ "decrements": 0,
|
| |
+ "increments": 1,
|
| |
+ "release": "f24",
|
| |
+ "total": 1,
|
| |
+ "username": "decause"
|
| |
+ }
|
| |
+ return MockResponse(json_data=data, status_code=200)
|
| |
+
|
| |
+ return MockResponse({}, 404)
|
| |
+
|
| |
+ @mock.patch('requests.get', side_effect=mocked_requests_get)
|
| |
+ def test_plus_plus_get_valid(self, mock_get):
|
| |
+ url = '/plus_plus/decause/status'
|
| |
+ result = self.app.get(url)
|
| |
+ expected = {
|
| |
+ "current": 0,
|
| |
+ "decrements": 0,
|
| |
+ "increments": 0,
|
| |
+ "release": "f24",
|
| |
+ "total": 0,
|
| |
+ "username": "decause"
|
| |
+ }
|
| |
+ self.assertEqual(json.loads(result.data), expected)
|
| |
+
|
| |
+ @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
+ def test_plus_plus_post_increment_valid(self, mock_post):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ url = '/plus_plus/decause/update'
|
| |
+ result = self.app.post(url, data={'increment': True})
|
| |
+ expected = {
|
| |
+ "current": 1,
|
| |
+ "decrements": 0,
|
| |
+ "increments": 1,
|
| |
+ "release": "f24",
|
| |
+ "total": 1,
|
| |
+ "username": "decause"
|
| |
+ }
|
| |
+ self.assertEqual(json.loads(result.data), expected)
|
| |
+
|
| |
+ @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
+ def test_plus_plus_post_increment_myself_error(self, mock_post):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ url = '/plus_plus/ralph/update'
|
| |
+ result = self.app.post(url, data={'increment': True})
|
| |
+ self.assertEqual(result.status_code, 403)
|
| |
+ self.assertEqual(result.data, 'You may not modify your own karma.')
|
| |
+
|
| |
+ @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
+ def test_plus_plus_post_increment_user_does_not_exist(self, mock_post):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ url = '/plus_plus/doesnotexist/update'
|
| |
+ result = self.app.post(url, data={'increment': True})
|
| |
+ self.assertEqual(result.status_code, 403)
|
| |
+ self.assertEqual(result.data, 'User does not exist')
|
| |
+
|
| |
+ @mock.patch('requests.post', side_effect=mocked_requests_post)
|
| |
+ def test_plus_plus_post_increment_no_data_error(self, mock_post):
|
| |
+ user = tests.FakeAuthorization('ralph')
|
| |
+ with tests.auth_set(app, user):
|
| |
+ url = '/plus_plus/decause/update'
|
| |
+ result = self.app.post(url, data={})
|
| |
+ self.assertEqual(result.status_code, 403)
|
| |
+ exp_str = "You must set 'decrement' or 'increment' " \
|
| |
+ "with a boolean value in the body"
|
| |
+ self.assertEqual(result.data, exp_str)
|
| |
+
|
| |
+ def test_plus_plus_receiver_does_not_exist(self):
|
| |
+ url = '/plus_plus/doesnotexist/status'
|
| |
+ result = self.app.get(url)
|
| |
+ self.assertEqual(result.status_code, 403)
|
| |
+ self.assertEqual(result.data, 'User does not exist')
|
| |
+
|
| |
+
|
| |
+
|
| |
|
| |
if __name__ == '__main__':
|
| |
unittest.main()
|
| |
all(e not in flask.request.form for e in ('increment', 'decrement'))
?