Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Issue #3321 - Convert tests to pytest for test_issues.py
1. It converts all the tests to pytest format
2. It adds new tests for handling the scenario which were not previously tested (as revealed by coverage).

issues.py is now 100% covered.
  • Loading branch information
karlcow committed Jun 1, 2020
commit 083209c647ceb085cf151769545628863b44ad62
228 changes: 131 additions & 97 deletions tests/unit/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,109 +6,143 @@

"""Tests for issue creation."""


import json
import requests
import unittest
from unittest.mock import patch
from unittest.mock import ANY

import pytest
from unittest.mock import MagicMock
from unittest.mock import patch
from werkzeug.datastructures import MultiDict
from werkzeug.exceptions import BadRequest

from .test_api_urls import mock_api_response
import webcompat
from webcompat import app
from webcompat.issues import moderation_template
from webcompat.issues import report_issue
from webcompat.issues import report_private_issue
from webcompat.issues import report_public_issue
from webcompat.issues import moderation_template


class TestIssue(unittest.TestCase):
"""Tests for issue creation."""

def setUp(self):
"""Set up the tests."""
pass

def tearDown(self):
"""Tear down the tests."""
pass

@patch.object(requests, 'post')
def test_report_issue_returns_number(self, mockpost):
"""Test we can expect an issue number back."""
with app.test_request_context():
mockpost.return_value.status_code = 201
mockpost.return_value.json.return_value = {'number': 2}
form = MultiDict([
('browser', 'Firefox 99.0'),
('description', 'sup'),
('details', ''),
('os', 'Mac OS X 13'),
('problem_category', 'unknown_bug'),
('submit_type', 'github-proxy-report'),
('url', 'http://3139.example.com'),
('username', ''), ])
rv = report_issue(form, True)
self.assertEqual(rv.get('number'), 2)

@patch.object(requests, 'post')
def test_report_private_issue_returns_nothing(self, mockpost):
"""Test that we get nothing back from a private issue report."""
with app.test_request_context():
mockpost.return_value.json.return_value = {'number': 2}
form = MultiDict([
('browser', 'Firefox 99.0'),
('description', 'sup'),
('details', ''),
('os', 'Mac OS X 13'),
('problem_category', 'unknown_bug'),
('submit_type', 'github-proxy-report'),
('url', 'http://3139.example.com'),
('username', ''), ])
rv = report_private_issue(form, 'http://public.example.com')
self.assertIsNone(rv)

@patch.object(requests, 'post')
def test_report_public_issue_returns_moderation_template(self, mockpost):
"""Test the data in report_public_issue contains the right data."""
with app.test_request_context():
report_public_issue()
args, kwargs = mockpost.call_args
post_data = json.loads(kwargs['data'])
self.assertIs(type(post_data), dict)
self.assertIn('title', post_data.keys())
self.assertIn('body', post_data.keys())
self.assertIn('labels', post_data.keys())
self.assertEqual(['action-needsmoderation'], post_data['labels'])

def test_moderation_template(self):
"""Check the moderation template structure.

- must be a dictionary
- must contain the keys: title, body
"""
actual = moderation_template('ongoing')
self.assertIs(type(actual), dict)
self.assertIn('title', actual.keys())
self.assertIn('body', actual.keys())

def test_moderation_template_rejected(self):
"""Check the return values are for the rejected case."""
actual = moderation_template('rejected')
self.assertEqual(actual['title'], 'Issue rejected.')
self.assertIn('Its original content has been deleted', actual['body'])

def test_moderation_template_ongoing(self):
"""Check the return values are for the needsmoderation case."""
# test the default
actual = moderation_template()
self.assertEqual(actual['title'], 'In the moderation queue.')
self.assertIn('put in the moderation queue.', actual['body'])
# test the keyword
actual = moderation_template('ongoing')
self.assertEqual(actual['title'], 'In the moderation queue.')
self.assertIn('put in the moderation queue.', actual['body'])
# bad keyword, we go back to the default.
actual = moderation_template('punkcat')
self.assertEqual(actual['title'], 'In the moderation queue.')
self.assertIn('put in the moderation queue.', actual['body'])
FORM = MultiDict([
('browser', 'Firefox 99.0'),
('description', 'sup'),
('details', ''),
('os', 'Mac OS X 13'),
('problem_category', 'unknown_bug'),
('submit_type', ''),
('url', 'http://3139.example.com'),
('username', ''), ])

FORM_ANON = FORM.copy()
FORM_ANON['submit_type'] = 'github-proxy-report'
FORM_AUTH = FORM.copy()
FORM_AUTH['submit_type'] = 'github-auth-report'


@pytest.fixture
def client():
Comment thread
karlcow marked this conversation as resolved.
Outdated
"""Test client for requests."""
webcompat.app.config['TESTING'] = True


def test_report_with_auth_on(client):
"""Test that we can still work with github-auth-report."""
with patch('webcompat.issues.github.post') as json_resp:
json_resp.return_value = {'number': 2}
json_data = report_issue(FORM_AUTH, proxy=False)
assert type(json_data) is dict
assert json_data.get('number') == 2


def test_report_issue_returns_number(client):
"""Test issue posting report the right json."""
with patch('webcompat.issues.proxy_request') as github_data:
github_data.return_value = mock_api_response({
'status_code': 201,
'content': '[{"number":2}]',
})
with patch('webcompat.issues.report_private_issue') as silent:
# We avoid making a call to report_private_issue
silent.return_value = None
json_data = report_issue(FORM_ANON, proxy=True)
assert type(json_data) is dict
assert json_data.get('number') == 2


def test_report_issue_fails_400_for_unknown_type(client):
"""Test request fails with a 400 because type unknown."""
with pytest.raises(BadRequest):
rv = report_issue(FORM, proxy=True)


def test_report_issue_fails_400_for_proxy(client):
"""Test github-proxy-report fails with a 400."""
with patch('webcompat.issues.proxy_request') as github_data:
github_data.return_value = mock_api_response({
'status_code': 400,
'content': '[{"number":2}]',
})
with pytest.raises(BadRequest):
rv = report_issue(FORM_ANON, proxy=True)


def test_report_private_issue_returns_nothing(client):
"""Test that we get nothing back from a private issue report."""
with patch('webcompat.issues.proxy_request') as github_data:
github_data.return_value = mock_api_response({
'status_code': 400,
'content': '[{"number":2}]',
})
rv = report_private_issue(FORM_ANON, 'http://public.example.com')
assert rv is None


def test_report_public_issue_returns_moderation_template(client):
"""Test the data in report_public_issue contains the right data."""
with patch('webcompat.issues.proxy_request') as req:
req.return_value = mock_api_response({
'status_code': 201,
'content': '[{"number":2}]',
})
report_public_issue()
args, kwargs = req.call_args
post_data = json.loads(kwargs['data'])
assert type(post_data) is dict
assert 'title' in post_data.keys()
assert 'body' in post_data.keys()
assert 'labels' in post_data.keys()
assert ['action-needsmoderation'] == post_data['labels']


def test_moderation_template(client):
"""Check the moderation template structure.

- must be a dictionary
- must contain the keys: title, body
"""
actual = moderation_template('ongoing')
assert type(actual) is dict
assert 'title' in actual.keys()
assert 'body' in actual.keys()


def test_moderation_template_rejected(client):
"""Check the return values are for the rejected case."""
actual = moderation_template('rejected')
assert actual['title'] == 'Issue rejected.'
assert 'Its original content has been deleted' in actual['body']


def test_moderation_template_ongoing(client):
"""Check the return values are for the needsmoderation case."""
# test the default
actual = moderation_template()
assert actual['title'] == 'In the moderation queue.'
assert 'put in the moderation queue.' in actual['body']
# test the keyword
actual = moderation_template('ongoing')
assert actual['title'] == 'In the moderation queue.'
assert 'put in the moderation queue.' in actual['body']
# bad keyword, we go back to the default.
actual = moderation_template('punkcat')
assert actual['title'] == 'In the moderation queue.'
assert 'put in the moderation queue.' in actual['body']