Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Merge pull request #284 from dhermes/100-percent-coverage-devshell
Browse files Browse the repository at this point in the history
100% test coverage for devshell module.
  • Loading branch information
nathanielmanistaatgoogle committed Aug 25, 2015
2 parents 7689d68 + a94c93b commit 6a9308a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
3 changes: 1 addition & 2 deletions oauth2client/devshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import json
import os
import socket

from oauth2client._helpers import _to_bytes
from oauth2client import client
Expand Down Expand Up @@ -71,8 +72,6 @@ def _SendRecv():
if port == 0:
raise NoDevshellServer()

import socket

sock = socket.socket()
sock.connect(('localhost', port))

Expand Down
84 changes: 84 additions & 0 deletions tests/test_devshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,93 @@

"""Tests for oauth2client.devshell."""

import json
import os
import socket
import threading
import unittest

import mock

from oauth2client._helpers import _to_bytes
from oauth2client.client import save_to_well_known_file
from oauth2client.devshell import _SendRecv
from oauth2client.devshell import CREDENTIAL_INFO_REQUEST_JSON
from oauth2client.devshell import CommunicationError
from oauth2client.devshell import CredentialInfoResponse
from oauth2client.devshell import DEVSHELL_ENV
from oauth2client.devshell import DevshellCredentials
from oauth2client.devshell import NoDevshellServer


class TestCredentialInfoResponse(unittest.TestCase):

def test_constructor_with_non_list(self):
json_non_list = '{}'
self.assertRaises(ValueError, CredentialInfoResponse,
json_non_list)

def test_constructor_with_bad_json(self):
json_non_list = '{BADJSON'
self.assertRaises(ValueError, CredentialInfoResponse,
json_non_list)

def test_constructor_empty_list(self):
info_response = CredentialInfoResponse('[]')
self.assertEqual(info_response.user_email, None)
self.assertEqual(info_response.project_id, None)
self.assertEqual(info_response.access_token, None)

def test_constructor_full_list(self):
user_email = 'user_email'
project_id = 'project_id'
access_token = 'access_token'
json_string = json.dumps([user_email, project_id, access_token])
info_response = CredentialInfoResponse(json_string)
self.assertEqual(info_response.user_email, user_email)
self.assertEqual(info_response.project_id, project_id)
self.assertEqual(info_response.access_token, access_token)


class Test_SendRecv(unittest.TestCase):

def test_port_zero(self):
with mock.patch('oauth2client.devshell.os') as os_mod:
os_mod.getenv = mock.MagicMock(name='getenv', return_value=0)
self.assertRaises(NoDevshellServer, _SendRecv)
os_mod.getenv.assert_called_once_with(DEVSHELL_ENV, 0)

def test_no_newline_in_received_header(self):
non_zero_port = 1
sock = mock.MagicMock()

header_without_newline = ''
sock.recv(6).decode = mock.MagicMock(
name='decode', return_value=header_without_newline)

with mock.patch('oauth2client.devshell.os') as os_mod:
os_mod.getenv = mock.MagicMock(name='getenv',
return_value=non_zero_port)
with mock.patch('oauth2client.devshell.socket') as socket:
socket.socket = mock.MagicMock(name='socket',
return_value=sock)
self.assertRaises(CommunicationError, _SendRecv)
os_mod.getenv.assert_called_once_with(DEVSHELL_ENV, 0)
socket.socket.assert_called_once_with()
sock.recv(6).decode.assert_called_once_with()

data = CREDENTIAL_INFO_REQUEST_JSON
msg = _to_bytes('%s\n%s' % (len(data), data), encoding='utf-8')
expected_sock_calls = [
mock.call.recv(6), # From the set-up above
mock.call.connect(('localhost', non_zero_port)),
mock.call.sendall(msg),
mock.call.recv(6),
mock.call.recv(6), # From the check above
]
self.assertEqual(sock.method_calls, expected_sock_calls)


class _AuthReferenceServer(threading.Thread):

def __init__(self, response=None):
Expand Down Expand Up @@ -138,3 +212,13 @@ def test_refuses_to_save_to_well_known_file(self):
save_to_well_known_file, creds)
finally:
os.path.isdir = ORIGINAL_ISDIR

def test_from_json(self):
self.assertRaises(NotImplementedError,
DevshellCredentials.from_json, None)

def test_serialization_data(self):
with _AuthReferenceServer('[]'):
credentials = DevshellCredentials()
self.assertRaises(NotImplementedError, getattr,
credentials, 'serialization_data')

0 comments on commit 6a9308a

Please sign in to comment.