python3: encode response.content in tests

This commit is contained in:
Frédéric Péters 2020-01-19 19:25:48 +01:00
parent 8ccbc23f45
commit b3534e1330
1 changed files with 28 additions and 27 deletions

View File

@ -22,6 +22,7 @@ import pytest
from django.core.urlresolvers import reverse
from django.test import override_settings
from django.utils import six
from django.utils.encoding import force_text
from django.utils.timezone import now, timedelta
from welco.sources.phone import models
@ -43,7 +44,7 @@ def test_call_start_stop(client):
content_type='application/json')
assert response.status_code == 200
assert response['content-type'] == 'application/json'
assert json.loads(response.content) == {'err': 0}
assert response.json() == {'err': 0}
assert models.PhoneCall.objects.count() == 1
assert models.PhoneCall.objects.filter(
caller='0033699999999',
@ -54,7 +55,7 @@ def test_call_start_stop(client):
content_type='application/json')
assert response.status_code == 200
assert response['content-type'] == 'application/json'
assert json.loads(response.content) == {'err': 0}
assert response.json() == {'err': 0}
assert models.PhoneCall.objects.count() == 2
assert models.PhoneCall.objects.filter(
caller='0033699999999',
@ -70,7 +71,7 @@ def test_call_start_stop(client):
content_type='application/json')
assert response.status_code == 200
assert response['content-type'] == 'application/json'
assert json.loads(response.content) == {'err': 0}
assert response.json() == {'err': 0}
assert models.PhoneCall.objects.count() == 2
assert models.PhoneCall.objects.filter(
caller='0033699999999',
@ -81,7 +82,7 @@ def test_call_start_stop(client):
content_type='application/json')
assert response.status_code == 200
assert response['content-type'] == 'application/json'
assert json.loads(response.content) == {'err': 0}
assert response.json() == {'err': 0}
assert models.PhoneCall.objects.count() == 2
assert models.PhoneCall.objects.filter(
caller='0033699999999',
@ -141,7 +142,7 @@ def test_current_calls(user, client):
content_type='application/json')
assert response.status_code == 200
assert response['content-type'] == 'application/json'
assert json.loads(response.content) == {'err': 0}
assert response.json() == {'err': 0}
# register user to some lines
# then remove from some
@ -153,7 +154,7 @@ def test_current_calls(user, client):
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
assert response['content-type'] == 'application/json'
payload = json.loads(response.content)
payload = response.json()
assert isinstance(payload, dict)
assert set(payload.keys()) == set(['err', 'data'])
assert payload['err'] == 0
@ -181,7 +182,7 @@ def test_current_calls(user, client):
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
assert response['content-type'] == 'application/json'
payload = json.loads(response.content)
payload = response.json()
assert isinstance(payload, dict)
assert set(payload.keys()) == set(['err', 'data'])
assert payload['err'] == 0
@ -202,7 +203,7 @@ def test_take_release_line(user, client):
content_type='application/json')
assert response.status_code == 200
assert response['content-type'] == 'application/json'
assert json.loads(response.content) == {'err': 0}
assert response.json() == {'err': 0}
assert models.PhoneLine.objects.count() == 1
assert models.PhoneLine.objects.filter(
users=user, callee='102').count() == 1
@ -210,7 +211,7 @@ def test_take_release_line(user, client):
content_type='application/json')
assert response.status_code == 200
assert response['content-type'] == 'application/json'
assert json.loads(response.content) == {'err': 0}
assert response.json() == {'err': 0}
assert models.PhoneLine.objects.count() == 1
assert models.PhoneLine.objects.filter(
users=user, callee='102').count() == 0
@ -220,18 +221,18 @@ def test_phone_zone(user, client):
client.login(username='toto', password='toto')
response = client.get(reverse('phone-zone'))
assert response.status_code == 200
assert 'You do not have a phoneline configured' in response.content
assert 'You do not have a phoneline configured' in force_text(response.content)
models.PhoneLine.take(callee='102', user=user)
response = client.get(reverse('phone-zone'))
assert response.status_code == 200
assert 'You do not have a phoneline configured' not in response.content
assert '<li>102' in response.content
assert 'data-callee="102"' in response.content
assert 'You do not have a phoneline configured' not in force_text(response.content)
assert '<li>102' in force_text(response.content)
assert 'data-callee="102"' in force_text(response.content)
currents = re.search('<div id="source-mainarea" '
'data-current-calls="/api/phone/current-calls/">'
'(.*?)</div>', response.content, flags=re.DOTALL)
'(.*?)</div>', force_text(response.content), flags=re.DOTALL)
assert currents.group(1).strip() == ''
# create a call
@ -241,7 +242,7 @@ def test_phone_zone(user, client):
assert response.status_code == 200
response = client.get(reverse('phone-zone'))
assert response.status_code == 200
assert '<h1>Current Call: <strong>003369999999</strong></h1>' in response.content
assert '<h1>Current Call: <strong>003369999999</strong></h1>' in force_text(response.content)
# simulate a mellon user
session = client.session
@ -249,19 +250,19 @@ def test_phone_zone(user, client):
session.save()
response = client.get(reverse('phone-zone'))
assert response.status_code == 200
assert 'agent007' not in response.content
assert 'data-callee="agent007"' not in response.content
assert '<li>102' in response.content
assert 'data-callee="102"' in response.content
assert 'agent007' not in force_text(response.content)
assert 'data-callee="agent007"' not in force_text(response.content)
assert '<li>102' in force_text(response.content)
assert 'data-callee="102"' in force_text(response.content)
with override_settings(PHONE_AUTOTAKE_MELLON_USERNAME=True):
response = client.get(reverse('phone-zone'))
assert response.status_code == 200
assert '<h1>Current Call: <strong>003369999999</strong></h1>' in response.content
assert 'agent007' in response.content
assert 'data-callee="agent007"' in response.content
assert '<li>102' in response.content
assert 'data-callee="102"' in response.content
assert '<h1>Current Call: <strong>003369999999</strong></h1>' in force_text(response.content)
assert 'agent007' in force_text(response.content)
assert 'data-callee="agent007"' in force_text(response.content)
assert '<li>102' in force_text(response.content)
assert 'data-callee="102"' in force_text(response.content)
def test_call_expiration(user, client):
@ -278,7 +279,7 @@ def test_call_expiration(user, client):
client.login(username='toto', password='toto')
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
payload = json.loads(response.content)
payload = response.json()
assert payload['err'] == 0
assert len(payload['data']['calls']) == 1
@ -289,7 +290,7 @@ def test_call_expiration(user, client):
# get list of calls without expiration
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
payload = json.loads(response.content)
payload = response.json()
assert payload['err'] == 0
assert len(payload['data']['calls']) == 1 # still here
@ -297,7 +298,7 @@ def test_call_expiration(user, client):
with override_settings(PHONE_MAX_CALL_DURATION=2):
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
payload = json.loads(response.content)
payload = response.json()
assert payload['err'] == 0
assert len(payload['data']['calls']) == 0 # call is expired