mocked all c2stuff except workout export
This commit is contained in:
@@ -424,6 +424,7 @@ def do_refresh_token(refreshtoken):
|
|||||||
prepped.body+=scope
|
prepped.body+=scope
|
||||||
|
|
||||||
response = s.send(prepped)
|
response = s.send(prepped)
|
||||||
|
|
||||||
token_json = response.json()
|
token_json = response.json()
|
||||||
try:
|
try:
|
||||||
thetoken = token_json['access_token']
|
thetoken = token_json['access_token']
|
||||||
@@ -461,19 +462,8 @@ def get_token(code):
|
|||||||
prepped.body+="&scope="
|
prepped.body+="&scope="
|
||||||
prepped.body+=scope
|
prepped.body+=scope
|
||||||
|
|
||||||
print prepped.body
|
|
||||||
|
|
||||||
response = s.send(prepped)
|
response = s.send(prepped)
|
||||||
|
|
||||||
with open("media/c2authorize.log","a") as f:
|
|
||||||
try:
|
|
||||||
f.write(reponse.status_code+"\n")
|
|
||||||
f.write(reponse.text+"\n")
|
|
||||||
f.write(response.json+"\n\n")
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
token_json = response.json()
|
token_json = response.json()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -541,6 +531,7 @@ def get_workout(user,c2id):
|
|||||||
splitdata = data['workout']['intervals']
|
splitdata = data['workout']['intervals']
|
||||||
|
|
||||||
# Check if workout has stroke data, and get the stroke data
|
# Check if workout has stroke data, and get the stroke data
|
||||||
|
|
||||||
if data['stroke_data']:
|
if data['stroke_data']:
|
||||||
res2 = get_c2_workout_strokes(user,c2id)
|
res2 = get_c2_workout_strokes(user,c2id)
|
||||||
if res2.status_code == 200:
|
if res2.status_code == 200:
|
||||||
@@ -692,7 +683,6 @@ def workout_c2_upload(user,w):
|
|||||||
c2id = 0
|
c2id = 0
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print response.status_code
|
|
||||||
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
|
||||||
c2id = 0
|
c2id = 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from __future__ import print_function
|
#from __future__ import print_function
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@@ -52,9 +52,7 @@ class DjangoTestCase(TestCase, MockTestCase):
|
|||||||
MockTestCase.tearDown(self)
|
MockTestCase.tearDown(self)
|
||||||
delete_strokedata(1)
|
delete_strokedata(1)
|
||||||
|
|
||||||
# Create your tests here.
|
def mocked_requests(*args, **kwargs):
|
||||||
|
|
||||||
def mocked_requests_get(*args, **kwargs):
|
|
||||||
with open('rowers/testdata/c2jsonworkoutdata.txt','r') as infile:
|
with open('rowers/testdata/c2jsonworkoutdata.txt','r') as infile:
|
||||||
c2workoutdata = json.load(infile)
|
c2workoutdata = json.load(infile)
|
||||||
|
|
||||||
@@ -71,11 +69,58 @@ def mocked_requests_get(*args, **kwargs):
|
|||||||
def json(self):
|
def json(self):
|
||||||
return self.json_data
|
return self.json_data
|
||||||
|
|
||||||
if 'log.concept2.com/' in args[0]:
|
class MockSession:
|
||||||
if 'strokes' in args[0]:
|
def send(self,prepped):
|
||||||
|
json_data = {
|
||||||
|
'access_token': 'TA3n1vrNjuQJWw0TdCDHnjSmrjIPULhTlejMIWqq',
|
||||||
|
'expires_in': 604800,
|
||||||
|
'refresh_token': 'jHJhFzCfOOKB8oyiayubhLAlxaMkG3ruC1E8YxaR'
|
||||||
|
}
|
||||||
|
|
||||||
|
return MockResponse(json_data,200)
|
||||||
|
|
||||||
|
if not args:
|
||||||
|
return MockSession()
|
||||||
|
|
||||||
|
c2tester = re.compile('.*?log\.concept2\.com')
|
||||||
|
|
||||||
|
c2importregex = '.*?concept2.com\/api\/users\/me\/results\/\d+'
|
||||||
|
c2importtester = re.compile(c2importregex)
|
||||||
|
|
||||||
|
c2strokesregex = '.*?concept2.com\/api\/users\/me\/results\/\d+\/strokes'
|
||||||
|
c2strokestester = re.compile(c2strokesregex)
|
||||||
|
|
||||||
|
c2workoutlistregex = '.*?concept2\.com\/api\/users\/me\/results\?page=\d'
|
||||||
|
c2workoutlisttester = re.compile(c2workoutlistregex)
|
||||||
|
|
||||||
|
if c2tester.match(args[0]):
|
||||||
|
if c2strokestester.match(args[0]):
|
||||||
return MockResponse(c2strokedata,200)
|
return MockResponse(c2strokedata,200)
|
||||||
elif 'page' in args[0]:
|
elif c2importtester.match(args[0]):
|
||||||
|
return MockResponse(c2workoutdata,200)
|
||||||
|
elif c2workoutlisttester(args[0]):
|
||||||
return MockResponse(c2workoutlist,200)
|
return MockResponse(c2workoutlist,200)
|
||||||
|
elif 'access_token' in args[0]:
|
||||||
|
json_data = {
|
||||||
|
'access_token': 'TA3n1vrNjuQJWw0TdCDHnjSmrjIPULhTlejMIWqq',
|
||||||
|
'expires_in': 604800,
|
||||||
|
'refresh_token': 'jHJhFzCfOOKB8oyiayubhLAlxaMkG3ruC1E8YxaR'
|
||||||
|
}
|
||||||
|
return MockResponse(json_data,200)
|
||||||
|
elif 'users/me' in args[0]:
|
||||||
|
json_data = {
|
||||||
|
'data': {
|
||||||
|
'username': 'john',
|
||||||
|
'id': 1234,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MockResponse(json_data,200)
|
||||||
|
elif 'results' in args[0]:
|
||||||
|
json_data = {
|
||||||
|
'data': {
|
||||||
|
'id': 1223,
|
||||||
|
}
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
return MockResponse(c2workoutdata,200)
|
return MockResponse(c2workoutdata,200)
|
||||||
|
|
||||||
@@ -101,14 +146,29 @@ class C2Objects(DjangoTestCase):
|
|||||||
self.r.save()
|
self.r.save()
|
||||||
self.c.login(username='john',password='koeinsloot')
|
self.c.login(username='john',password='koeinsloot')
|
||||||
|
|
||||||
|
@patch('rowers.c2stuff.requests.Session', side_effect=mocked_requests)
|
||||||
|
def test_c2_callback(self, mock_post):
|
||||||
|
response = self.c.get('/call_back?code=dsdoij232s',follow=True)
|
||||||
|
|
||||||
@patch('rowers.c2stuff.requests.get', side_effect=mocked_requests_get)
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
@patch('rowers.c2stuff.Session', side_effect=mocked_requests)
|
||||||
|
def test_c2_token_refresh(self, mock_post):
|
||||||
|
response = self.c.get('/rowers/me/c2refresh/',follow=True)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@patch('rowers.c2stuff.requests.post', side_effect=mocked_requests)
|
||||||
def test_c2_list(self, mock_get):
|
def test_c2_list(self, mock_get):
|
||||||
response = self.c.get('/rowers/workout/c2list',follow=True)
|
response = self.c.get('/rowers/workout/c2list',follow=True)
|
||||||
|
|
||||||
self.assertEqual(response.status_code,200)
|
self.assertEqual(response.status_code,200)
|
||||||
|
|
||||||
@patch('rowers.c2stuff.requests.get', side_effect=mocked_requests_get)
|
@patch('rowers.c2stuff.requests.get', side_effect=mocked_requests)
|
||||||
def test_c2_import(self, mock_get):
|
def test_c2_import(self, mock_get):
|
||||||
|
|
||||||
response = self.c.get('/rowers/workout/c2import/12/',follow=True)
|
response = self.c.get('/rowers/workout/c2import/12/',follow=True)
|
||||||
|
|||||||
@@ -1993,6 +1993,7 @@ def rower_tp_authorize(request):
|
|||||||
def rower_c2_token_refresh(request):
|
def rower_c2_token_refresh(request):
|
||||||
r = getrower(request.user)
|
r = getrower(request.user)
|
||||||
res = c2stuff.do_refresh_token(r.c2refreshtoken)
|
res = c2stuff.do_refresh_token(r.c2refreshtoken)
|
||||||
|
|
||||||
if res[0] != None:
|
if res[0] != None:
|
||||||
access_token = res[0]
|
access_token = res[0]
|
||||||
expires_in = res[1]
|
expires_in = res[1]
|
||||||
@@ -2009,7 +2010,7 @@ def rower_c2_token_refresh(request):
|
|||||||
messages.info(request,successmessage)
|
messages.info(request,successmessage)
|
||||||
else:
|
else:
|
||||||
message = "Something went wrong (refreshing tokens). Please reauthorize:"
|
message = "Something went wrong (refreshing tokens). Please reauthorize:"
|
||||||
messages.error(request,successmessage)
|
messages.error(request,message)
|
||||||
|
|
||||||
return imports_view(request)
|
return imports_view(request)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user