forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee273e1
commit 115a11a
Showing
5 changed files
with
53 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
# limitations under the License. | ||
|
||
import logging | ||
from contextlib import nested | ||
import os | ||
|
||
from core.domain import collection_services | ||
|
@@ -139,39 +138,57 @@ def test_get_user_id_from_username(self): | |
|
||
def test_fetch_gravatar_success(self): | ||
user_email = '[email protected]' | ||
gravatar_filepath = os.path.join( | ||
expected_gravatar_filepath = os.path.join( | ||
'static', 'images', 'avatar', 'gravatar_example.png') | ||
with open(gravatar_filepath, 'r') as f: | ||
with open(expected_gravatar_filepath, 'r') as f: | ||
gravatar = f.read() | ||
with self.urlfetch_mock(content=gravatar): | ||
profile_picture = user_services.fetch_gravatar(user_email) | ||
gravatar_data_url = utils.convert_png_to_data_url(gravatar_filepath) | ||
gravatar_data_url = utils.convert_png_to_data_url( | ||
expected_gravatar_filepath) | ||
self.assertEqual(profile_picture, gravatar_data_url) | ||
|
||
def test_fetch_gravatar_failure_404(self): | ||
user_email = '[email protected]' | ||
logging_error_mock = test_utils.CallCounter(lambda x: x) | ||
error_messages = [] | ||
def log_mock(message): | ||
error_messages.append(message) | ||
|
||
gravatar_url = user_services.get_gravatar_url(user_email) | ||
expected_error_message = ( | ||
'[Status 404] Failed to fetch Gravatar from %s' % gravatar_url) | ||
logging_error_mock = test_utils.CallCounter(log_mock) | ||
urlfetch_counter = test_utils.CallCounter(urlfetch.fetch) | ||
with nested( | ||
self.urlfetch_mock(status_code=404), | ||
self.swap(logging, 'error', logging_error_mock), | ||
self.swap(urlfetch, 'fetch', urlfetch_counter)): | ||
urlfetch_mock_ctx = self.urlfetch_mock(status_code=404) | ||
log_swap_ctx = self.swap(logging, 'error', logging_error_mock) | ||
fetch_swap_ctx = self.swap(urlfetch, 'fetch', urlfetch_counter) | ||
with urlfetch_mock_ctx, log_swap_ctx, fetch_swap_ctx: | ||
profile_picture = user_services.fetch_gravatar(user_email) | ||
self.assertEqual(urlfetch_counter.times_called, 1) | ||
self.assertEqual(logging_error_mock.times_called, 1) | ||
self.assertEqual(expected_error_message, error_messages[0]) | ||
self.assertEqual( | ||
profile_picture, user_services.DEFAULT_IDENTICON_DATA_URL) | ||
|
||
def test_fetch_gravatar_failure_exception(self): | ||
user_email = '[email protected]' | ||
logging_error_mock = test_utils.CallCounter(lambda x: x) | ||
error_messages = [] | ||
def log_mock(message): | ||
error_messages.append(message) | ||
|
||
gravatar_url = user_services.get_gravatar_url(user_email) | ||
expected_error_message = ( | ||
'Failed to fetch Gravatar from %s' % gravatar_url) | ||
logging_error_mock = test_utils.CallCounter(log_mock) | ||
urlfetch_fail_mock = test_utils.FailingFunction( | ||
urlfetch.fetch, urlfetch.InvalidURLError, 'infinity') | ||
with nested( | ||
self.swap(logging, 'error', logging_error_mock), | ||
self.swap(urlfetch, 'fetch', urlfetch_fail_mock)): | ||
urlfetch.fetch, urlfetch.InvalidURLError, | ||
test_utils.FailingFunction.INFINITY) | ||
log_swap_ctx = self.swap(logging, 'error', logging_error_mock) | ||
fetch_swap_ctx = self.swap(urlfetch, 'fetch', urlfetch_fail_mock) | ||
with log_swap_ctx, fetch_swap_ctx: | ||
profile_picture = user_services.fetch_gravatar(user_email) | ||
self.assertEqual(logging_error_mock.times_called, 1) | ||
self.assertEqual(expected_error_message, error_messages[0]) | ||
self.assertEqual( | ||
profile_picture, user_services.DEFAULT_IDENTICON_DATA_URL) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters