def test_that_we_can_add_a_comment_to_a_bug_before_it_is_put(): responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/login?login=foo&password=bar", body='{"token": "foobar"}', status=200, content_type="application/json", match_querystring=True, ) responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar&include_fields=version&include_fields=id&include_fields=summary&include_fields=status&include_fields=op_sys&include_fields=resolution&include_fields=product&include_fields=component&include_fields=platform", body=json.dumps(example_return), status=200, content_type="application/json", match_querystring=True, ) bugzilla = Bugsy("foo", "bar") bug = Bug() bug.summary = "I like cheese" bug.add_comment("I like sausages") bug_dict = bug.to_dict().copy() bug_dict["id"] = 123123 responses.add( responses.POST, "https://bugzilla.mozilla.org/rest/bug?token=foobar", body=json.dumps(bug_dict), status=200, content_type="application/json", match_querystring=True, ) bugzilla.put(bug)
def test_we_cant_post_without_a_username_or_password(): bugzilla = Bugsy() try: bugzilla.put("foo") assert 1 == 0, "Should have thrown when calling put" except BugsyException as e: assert str(e) == "Message: Unfortunately you can't put bugs in Bugzilla without credentials"
def test_that_we_can_add_a_comment_to_an_existing_bug(): responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/login?login=foo&password=bar", body='{"token": "foobar"}', status=200, content_type="application/json", match_querystring=True, ) responses.add( responses.GET, rest_url("bug", 1017315, token="foobar"), body=json.dumps(example_return), status=200, content_type="application/json", match_querystring=True, ) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) responses.add( responses.POST, "https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar", body=json.dumps({}), status=200, content_type="application/json", match_querystring=True, ) bug.add_comment("I like sausages") assert len(responses.calls) == 3
def test_comment_retrieval(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017315, token='foobar'), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body=json.dumps(comments_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) comments = bug.get_comments() assert len(comments) == 2 c1 = comments[0] assert c1.attachment_id is None assert c1.author == u'*****@*****.**' assert c1.bug_id == 1017315 assert c1.creation_time == datetime.datetime(2014, 03, 27, 23, 47, 45) assert c1.creator == u'*****@*****.**' assert c1.id == 8589785 assert c1.is_private is False assert c1.text == u'text 1' assert c1.tags == set([u'tag1', u'tag2']) assert c1.time == datetime.datetime(2014, 03, 27, 23, 47, 45)
def test_we_can_update_a_bug_from_bugzilla(): responses.add( responses.GET, rest_url("bug", 1017315), body=json.dumps(example_return), status=200, content_type="application/json", match_querystring=True, ) bugzilla = Bugsy() bug = bugzilla.get(1017315) import copy bug_dict = copy.deepcopy(example_return) bug_dict["bugs"][0]["status"] = "REOPENED" responses.reset() responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/bug/1017315", body=json.dumps(bug_dict), status=200, content_type="application/json", ) bug.update() assert bug.status == "REOPENED"
def test_an_exception_is_raised_when_we_hit_an_error(): responses.add(responses.GET, rest_url('bug', 1017315), body="It's all broken", status=500, content_type='application/json', match_querystring=True) bugzilla = Bugsy() with pytest.raises(BugsyException) as e: bugzilla.get(1017315) assert str(e.value) == "Message: We received a 500 error with the following: It's all broken"
def test_bugsyexception_raised_for_http_502_when_retrieving_bugs(): responses.add(responses.GET, rest_url('bug', 123456), body='Bad Gateway', status=502, content_type='text/html', match_querystring=True) bugzilla = Bugsy() with pytest.raises(BugsyException) as e: r = bugzilla.get(123456) assert str(e.value) == "Message: We received a 502 error with the following: Bad Gateway"
def test_we_can_get_a_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?include_fields=version&include_fields=id&include_fields=summary&include_fields=status&include_fields=op_sys&include_fields=resolution&include_fields=product&include_fields=component&include_fields=platform', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bug = bugzilla.get(1017315) assert bug.id == 1017315 assert bug.status == 'RESOLVED' assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'
def test_we_can_get_a_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bug = bugzilla.get(1017315) assert bug.id == 1017315 assert bug.status == 'RESOLVED' assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'
def test_we_cant_post_without_passing_a_bug_object(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") try: bugzilla.put("foo") assert 1 == 0, "Should have thrown an error about type when calling put" except BugsyException as e: assert str(e) == "Message: Please pass in a Bug object when posting to Bugzilla"
def test_we_can_get_a_bug_with_login_token(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) assert bug.id == 1017315 assert bug.status == 'RESOLVED' assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'
def test_we_can_get_a_bug_with_login_token(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017315), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) assert bug.id == 1017315 assert bug.status == 'RESOLVED' assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar' assert responses.calls[1].request.headers['X-Bugzilla-Token'] == 'foobar'
def test_we_can_create_a_new_remote_bug(): bug = Bug() bug.summary = "I like foo" responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) bug_dict = bug.to_dict().copy() bug_dict['id'] = 123123 responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug', body=json.dumps(bug_dict), status=200, content_type='application/json') bugzilla = Bugsy("foo", "bar") bugzilla.put(bug) assert bug.id != None
def test_we_handle_errors_from_bugzilla_when_posting(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug', body='{"error":true,"code":50,"message":"You must select/enter a component."}', status=200, content_type='application/json') bugzilla = Bugsy("foo", "bar") bug = Bug() try: bugzilla.put(bug) assert 1 == 0, "Put should have raised an error" except BugsyException as e: assert str(e) == "Message: You must select/enter a component."
def test_we_can_update_a_bug_from_bugzilla(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bug = bugzilla.get(1017315) import copy bug_dict = copy.deepcopy(example_return) bug_dict['bugs'][0]['status'] = "REOPENED" responses.reset() responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_dict), status=200, content_type='application/json') bug.update() assert bug.status == 'REOPENED'
def test_we_can_update_a_bug_from_bugzilla(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?include_fields=version&include_fields=id&include_fields=summary&include_fields=status&include_fields=op_sys&include_fields=resolution&include_fields=product&include_fields=component&include_fields=platform', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bug = bugzilla.get(1017315) import copy bug_dict = copy.deepcopy(example_return) bug_dict['bugs'][0]['status'] = "REOPENED" responses.reset() responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_dict), status=200, content_type='application/json') bug.update() assert bug.status == 'REOPENED'
def test_that_we_can_add_a_comment_to_a_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) bug.add_comment("I like sausages") responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla.put(bug)
def test_we_can_put_a_current_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) bug_dict = example_return.copy() bug_dict['summary'] = 'I love foo but hate bar' responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_dict), status=200, content_type='application/json') bugzilla = Bugsy("foo", "bar") bug = Bug(**example_return['bugs'][0]) bug.summary = 'I love foo but hate bar' bugzilla.put(bug) assert bug.summary == 'I love foo but hate bar'
def test_bugsyexception_raised_for_http_500_when_commenting_on_a_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017315, token='foobar'), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body='Internal Server Error', status=500, content_type='text/html', match_querystring=True) with pytest.raises(BugsyException) as e: bug.add_comment("I like sausages") assert str(e.value) == "Message: We received a 500 error with the following: Internal Server Error"
def test_we_handle_errors_from_bugzilla_when_updating_a_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug/1017315', body='{"error":true,"code":50,"message":"You must select/enter a component."}', status=200, content_type='application/json') bugzilla = Bugsy("foo", "bar") bug_dict = example_return.copy() bug_dict['summary'] = 'I love foo but hate bar' bug = Bug(**bug_dict['bugs'][0]) try: bugzilla.put(bug) except BugsyException as e: assert str(e) == "Message: You must select/enter a component."
def test_we_can_handle_errors_when_retrieving_bugs(): error_response = { "code" : 101, "documentation" : "http://www.bugzilla.org/docs/tip/en/html/api/", "error" : True, "message" : "Bug 111111111111 does not exist." } responses.add(responses.GET, rest_url('bug', 111111111), body=json.dumps(error_response), status=404, content_type='application/json', match_querystring=True) bugzilla = Bugsy() try: bug = bugzilla.get(111111111) assert False, "A BugsyException should have been thrown" except BugsyException as e: assert str(e) == "Message: Bug 111111111111 does not exist." except Exception as e: assert False, "Wrong type of exception was thrown"
def test_that_we_can_add_a_comment_to_an_existing_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar&include_fields=version&include_fields=id&include_fields=summary&include_fields=status&include_fields=op_sys&include_fields=resolution&include_fields=product&include_fields=component&include_fields=platform', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body=json.dumps({}), status=200, content_type='application/json', match_querystring=True) bug.add_comment("I like sausages") assert len(responses.calls) == 3
def test_we_raise_an_exception_if_commenting_on_a_bug_that_returns_an_error(): responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/login?login=foo&password=bar", body='{"token": "foobar"}', status=200, content_type="application/json", match_querystring=True, ) responses.add( responses.GET, rest_url("bug", 1017315, token="foobar"), body=json.dumps(example_return), status=200, content_type="application/json", match_querystring=True, ) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) # will now return the following error. This could happen if the bug was open # when we did a `get()` but is now hidden error_response = { "code": 101, "message": "Bug 1017315 does not exist.", "documentation": "http://www.bugzilla.org/docs/tip/en/html/api/", "error": True, } responses.add( responses.POST, "https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar", body=json.dumps(error_response), status=404, content_type="application/json", match_querystring=True, ) try: bug.add_comment("I like sausages") assert False, "Should have raised an BugException for the bug not existing" except BugsyException as e: assert str(e) == "Message: Bug 1017315 does not exist. Code: 101" assert len(responses.calls) == 3
def test_validate_api_key(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/valid_login?login=foo', body='true', status=200, content_type='application/json', match_querystring=True) Bugsy(username='******', api_key='goodkey') assert ( responses.calls[0].request.headers['X-Bugzilla-API-Key'] == 'goodkey')
def test_we_can_set_the_user_agent_to_bugsy(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) Bugsy("foo", "bar") assert responses.calls[0].request.headers['User-Agent'] == "Bugsy"
def test_we_can_remove_tags_to_bug_comments(): responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/login?login=foo&password=bar", body='{"token": "foobar"}', status=200, content_type="application/json", match_querystring=True, ) responses.add( responses.GET, rest_url("bug", 1017315, token="foobar"), body=json.dumps(example_return), status=200, content_type="application/json", match_querystring=True, ) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar", body=json.dumps(comments_return), status=200, content_type="application/json", match_querystring=True, ) comments = bug.get_comments() responses.add( responses.PUT, "https://bugzilla.mozilla.org/rest/bug/comment/8589785/tags?token=foobar", body=json.dumps(["spam", "foo"]), status=200, content_type="application/json", match_querystring=True, ) comments[0].remove_tags("foo") assert len(responses.calls) == 4
def test_we_can_get_username_with_userid_cookie(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/user/1234?token=1234-abcd', body='{"users": [{"name": "*****@*****.**"}]}', status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy(userid='1234', cookie='abcd') assert bugzilla.username == '*****@*****.**'
def test_we_can_create_a_new_remote_bug(): bug = Bug() bug.summary = "I like foo" responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) bug_dict = bug.to_dict().copy() bug_dict['id'] = 123123 responses.add(responses.POST, 'https://bugzilla.mozilla.org/rest/bug', body=json.dumps(bug_dict), status=200, content_type='application/json') bugzilla = Bugsy("foo", "bar") bugzilla.put(bug) assert bug.id != None
def test_bad_api_key(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/valid_login?login=foo&api_key=badkey', body='{"documentation":"http://www.bugzilla.org/docs/tip/en/html/api/","error":true,"code":306,"message":"The API key you specified is invalid. Please check that you typed it correctly."}', status=400, content_type='application/json', match_querystring=True) try: Bugsy(username='******', api_key='badkey') assert False, 'Should have thrown' except LoginException as e: assert str(e) == 'Message: The API key you specified is invalid. Please check that you typed it correctly. Code: 306'
def test_we_can_put_a_current_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) bug_dict = example_return.copy() bug_dict['summary'] = 'I love foo but hate bar' responses.add(responses.PUT, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_dict), status=200, content_type='application/json') responses.add(responses.GET, rest_url('bug', 1017315, token="foobar"), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = Bug(**example_return['bugs'][0]) bug.summary = 'I love foo but hate bar' bug.assigned_to = "*****@*****.**" bugzilla.put(bug) assert bug.summary == 'I love foo but hate bar' assert bug.assigned_to == "*****@*****.**"
def test_we_can_put_a_current_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) bug_dict = example_return.copy() bug_dict['summary'] = 'I love foo but hate bar' responses.add(responses.PUT, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_dict), status=200, content_type='application/json') responses.add(responses.GET, rest_url('bug', 1017315), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = Bug(**example_return['bugs'][0]) bug.summary = 'I love foo but hate bar' bug.assigned_to = "*****@*****.**" bugzilla.put(bug) assert bug.summary == 'I love foo but hate bar' assert bug.assigned_to == "*****@*****.**"
def test_we_can_update_a_bug_from_bugzilla(bug_return): responses.add(responses.GET, rest_url('bug', 1017315), body=json.dumps(bug_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bug = bugzilla.get(1017315) responses.reset() responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_return), status=200, content_type='application/json') clone = Bug(bugsy=bugzilla, **bug.to_dict()) clone.status = 'NEW' clone.update() assert clone.id == 1017315 assert clone.status == 'RESOLVED'
def test_bugsyexception_raised_for_http_503_when_verifying_api_key(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/valid_login', body='Service Unavailable', status=503, content_type='text/html') with pytest.raises(BugsyException) as e: Bugsy(username='******', api_key='goodkey') assert str( e.value ) == "Message: We received a 503 error with the following: Service Unavailable Code: None"
def test_we_can_update_a_bug_from_bugzilla(): responses.add(responses.GET, rest_url('bug', 1017315), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bug = bugzilla.get(1017315) import copy bug_dict = copy.deepcopy(example_return) bug_dict['bugs'][0]['status'] = "REOPENED" responses.reset() responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_dict), status=200, content_type='application/json') bug.update() assert bug.status == 'REOPENED'
def test_we_raise_an_exception_when_getting_comments_and_bugzilla_errors(): responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/login?login=foo&password=bar", body='{"token": "foobar"}', status=200, content_type="application/json", match_querystring=True, ) responses.add( responses.GET, rest_url("bug", 1017315, token="foobar"), body=json.dumps(example_return), status=200, content_type="application/json", match_querystring=True, ) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) error_response = { "code": 67399, "message": "The requested method 'Bug.comments' was not found.", "documentation": u"http://www.bugzilla.org/docs/tip/en/html/api/", "error": True, } responses.add( responses.GET, "https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar", body=json.dumps(error_response), status=400, content_type="application/json", match_querystring=True, ) try: comments = bug.get_comments() assert False, "Should have raised an BugException for the bug not existing" except BugsyException as e: assert str(e) == "Message: The requested method 'Bug.comments' was not found. Code: 67399"
def test_we_get_a_login_exception_when_details_are_wrong(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login', body='{"message": "The username or password you entered is not valid."}', status=400, content_type='application/json', match_querystring=True) try: Bugsy("foo", "bar") assert 1 == 0, "Should have thrown an error" except LoginException as e: assert str(e) == "Message: The username or password you entered is not valid. Code: None" assert responses.calls[0].request.headers['X-Bugzilla-Login'] == 'foo' assert (responses.calls[0].request.headers['X-Bugzilla-Password'] == 'bar')
def test_we_can_get_a_bug_with_login_token(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar&include_fields=version&include_fields=id&include_fields=summary&include_fields=status&include_fields=op_sys&include_fields=resolution&include_fields=product&include_fields=component&include_fields=platform', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) assert bug.id == 1017315 assert bug.status == 'RESOLVED' assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'
def test_we_can_update_a_bug_from_bugzilla(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?include_fields=version&include_fields=id&include_fields=summary&include_fields=status&include_fields=op_sys&include_fields=resolution&include_fields=product&include_fields=component&include_fields=platform', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bug = bugzilla.get(1017315) import copy bug_dict = copy.deepcopy(example_return) bug_dict['bugs'][0]['status'] = "REOPENED" responses.reset() responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315', body=json.dumps(bug_dict), status=200, content_type='application/json') bug.update() assert bug.status == 'REOPENED'
def test_we_can_update_a_bug_with_login_token(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) import copy bug_dict = copy.deepcopy(example_return) bug_dict['bugs'][0]['status'] = "REOPENED" responses.reset() responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar', body=json.dumps(bug_dict), status=200, content_type='application/json', match_querystring=True) bug.update() assert bug.id == 1017315 assert bug.status == 'REOPENED' assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'
def test_we_can_handle_errors_when_retrieving_bugs(): error_response = { "code": 101, "documentation": "http://www.bugzilla.org/docs/tip/en/html/api/", "error": True, "message": "Bug 111111111111 does not exist." } responses.add(responses.GET, rest_url('bug', 111111111), body=json.dumps(error_response), status=404, content_type='application/json', match_querystring=True) bugzilla = Bugsy() try: bug = bugzilla.get(111111111) assert False, "A BugsyException should have been thrown" except BugsyException as e: assert str(e) == "Message: Bug 111111111111 does not exist. Code: 101" except Exception as e: assert False, "Wrong type of exception was thrown"
def test_we_raise_an_exception_if_commenting_on_a_bug_that_returns_an_error(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017315, token='foobar'), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) # will now return the following error. This could happen if the bug was open # when we did a `get()` but is now hidden error_response = { 'code': 101, 'message': 'Bug 1017315 does not exist.', 'documentation': 'http://www.bugzilla.org/docs/tip/en/html/api/', 'error': True } responses.add( responses.POST, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body=json.dumps(error_response), status=404, content_type='application/json', match_querystring=True) try: bug.add_comment("I like sausages") assert False, "Should have raised an BugException for the bug not existing" except BugsyException as e: assert str(e) == "Message: Bug 1017315 does not exist. Code: 101" assert len(responses.calls) == 3
def main(argv=None): """ Launch Bugmon :param argv: Command line to use instead of sys.argv (optional) :return: """ console_init_logging() args = parse_args(argv) # Get the API root, default to bugzilla.mozilla.org api_root = os.environ.get("BZ_API_ROOT") api_key = os.environ.get("BZ_API_KEY") if api_root is None or api_key is None: raise BugException("BZ_API_ROOT and BZ_API_KEY must be set!") bugsy = Bugsy(api_key=api_key, bugzilla_url=api_root) if args.bugs: bug_list = ",".join(args.bugs) params = {"id": bug_list} else: with open(args.search) as f: params = json.load(f) params["include_fields"] = "_default" response = bugsy.request("bug", params=params) bugs = [EnhancedBug(bugsy, **bug) for bug in response["bugs"]] for bug in bugs: with tempfile.TemporaryDirectory() as temp_dir: try: bugmon = BugMonitor(bugsy, bug, temp_dir, args.dry_run) log.info(f"Analyzing bug {bug.id} " f"(Status: {bugmon.bug.status}, " f"Resolution: {bugmon.bug.resolution})") bugmon.process() except BugException as e: log.error(f"Error processing bug {bug.id}: {e}")
def test_bugsyexception_raised_for_http_500_when_adding_tags_to_bug_comments(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017315, token='foobar'), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body=json.dumps(comments_return), status=200, content_type='application/json', match_querystring=True) comments = bug.get_comments() responses.add( responses.PUT, 'https://bugzilla.mozilla.org/rest/bug/comment/8589785/tags?token=foobar', body='Internal Server Error', status=500, content_type='text/html', match_querystring=True) with pytest.raises(BugsyException) as e: comments[0].add_tags("foo") assert str( e.value ) == "Message: We received a 500 error with the following: Internal Server Error Code: None"
def test_we_handle_errors_from_bugzilla_when_posting(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add( responses.POST, 'https://bugzilla.mozilla.org/rest/bug', body= '{"error":true,"code":50,"message":"You must select/enter a component."}', status=400, content_type='application/json') bugzilla = Bugsy("foo", "bar") bug = Bug() try: bugzilla.put(bug) assert 1 == 0, "Put should have raised an error" except BugsyException as e: assert str(e) == "Message: You must select/enter a component. Code: 50"
def test_we_raise_an_exception_when_getting_comments_and_bugzilla_errors(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017315, token='foobar'), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) error_response = { 'code': 67399, 'message': "The requested method 'Bug.comments' was not found.", 'documentation': u'http://www.bugzilla.org/docs/tip/en/html/api/', 'error': True } responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body=json.dumps(error_response), status=400, content_type='application/json', match_querystring=True) try: comments = bug.get_comments() assert False, "Should have raised an BugException for the bug not existing" except BugsyException as e: assert str( e ) == "Message: The requested method 'Bug.comments' was not found. Code: 67399"
def test_comment_retrieval(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315?token=foobar&include_fields=version&include_fields=id&include_fields=summary&include_fields=status&include_fields=op_sys&include_fields=resolution&include_fields=product&include_fields=component&include_fields=platform', body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body=json.dumps(comments_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) comments = bug.get_comments() assert len(comments) == 2 c1 = comments[0] assert c1.attachment_id is None assert c1.author == u'*****@*****.**' assert c1.bug_id == 1017315 assert c1.creation_time == datetime.datetime(2014, 03, 27, 23, 47, 45) assert c1.creator == u'*****@*****.**' assert c1.id == 8589785 assert c1.is_private is False assert c1.text == u'text 1' assert c1.tags == set([u'tag1', u'tag2']) assert c1.time == datetime.datetime(2014, 03, 27, 23, 47, 45)
def test_we_can_remove_tags_to_bug_comments(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017315, token='foobar'), body=json.dumps(example_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy("foo", "bar") bug = bugzilla.get(1017315) responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/bug/1017315/comment?token=foobar', body=json.dumps(comments_return), status=200, content_type='application/json', match_querystring=True) comments = bug.get_comments() responses.add( responses.PUT, 'https://bugzilla.mozilla.org/rest/bug/comment/8589785/tags?token=foobar', body=json.dumps(["spam", "foo"]), status=200, content_type='application/json', match_querystring=True) comments[0].remove_tags("foo") assert len(responses.calls) == 4
def test_we_handle_errors_from_bugzilla_when_updating_a_bug(): responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login', body='{"token": "foobar"}', status=200, content_type='application/json', match_querystring=True) responses.add( responses.PUT, 'https://bugzilla.mozilla.org/rest/bug/1017315', body= '{"error":true,"code":50,"message":"You must select/enter a component."}', status=400, content_type='application/json') bugzilla = Bugsy("foo", "bar") bug_dict = example_return.copy() bug_dict['summary'] = 'I love foo but hate bar' bug = Bug(**bug_dict['bugs'][0]) try: bugzilla.put(bug) except BugsyException as e: assert str(e) == "Message: You must select/enter a component. Code: 50"
def test_update_without_an_id_fails(attachment_return): data = copy.deepcopy(attachment_return['bugs']['1017315'][0]) resp_dict = {'attachments': [data]} resp_dict['attachments'][0]['id'] = None bugzilla = Bugsy() attachment = Attachment(bugzilla, **resp_dict) try: attachment.update() assert False, "Should have raised an AttachmentException due to update without id" except AttachmentException as e: assert str( e ) == "Message: Cannot update bug without an attachment id Code: None"
def test_we_can_return_keyword_search(): keyword_return = { "bugs": [{ "component": "Networking: HTTP", "product": "Core", "summary": "IsPending broken for requests without Content-Type" }, { "component": "Developer Tools: Graphic Commandline and Toolbar", "product": "Firefox", "summary": "GCLI Command to open Profile Directory" }, { "component": "Video/Audio Controls", "product": "Toolkit", "summary": "Fullscreen video should disable screensaver during playback on Linux" }, { "component": "Reader Mode", "product": "Firefox for Android", "summary": "Article showing twice in reader mode" }, { "component": "Message Reader UI", "product": "Thunderbird", "summary": "Make \"visited link\" coloring work in thunderbird" }] } responses.add(responses.GET, rest_url('bug', keywords='checkin-needed'), body=json.dumps(keyword_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bugs = bugzilla.search_for\ .keywords('checkin-needed')\ .search() assert len(responses.calls) == 1 assert len(bugs) == 5 assert bugs[0].product == keyword_return['bugs'][0]['product'] assert bugs[0].component == keyword_return['bugs'][0]['component']
def test_setter_validate_base64_types(attachment_return): bugzilla = Bugsy() source = attachment_return['bugs']['1017315'][0] attachment = Attachment(bugzilla, **source) attachment.data = 'Rm9vYmFy' assert attachment.data == 'Rm9vYmFy' try: attachment.data = 'foobar' assert False, "Should have raised an AttachmentException due to invalid data value" except AttachmentException as e: assert str( e ) == "Message: The data field value must be in base64 format Code: None"
def test_setter_validate_string_types(attachment_return): bugzilla = Bugsy() source = attachment_return['bugs']['1017315'][0] attachment = Attachment(bugzilla, **source) for field in ['comment', 'content_type', 'file_name', 'summary']: setattr(attachment, field, 'foobar') assert getattr(attachment, field) == 'foobar' try: setattr(attachment, field, 1) assert False, "Should have raised an AttachmentException due to invalid data value" except AttachmentException as e: match = "Message: The %s field value must be of type string Code: None" % field assert str(e) == match
def test_we_get_a_login_exception_when_details_are_wrong(): responses.add( responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=bar', body= '{"message": "The username or password you entered is not valid."}', status=200, content_type='application/json', match_querystring=True) try: Bugsy("foo", "bar") assert 1 == 0, "Should have thrown an error" except LoginException as e: assert str( e) == "Message: The username or password you entered is not valid."
def test_setter_validate_boolean_types(attachment_return): bugzilla = Bugsy() source = attachment_return['bugs']['1017315'][0] attachment = Attachment(bugzilla, **source) for field in ['is_patch', 'is_private']: setattr(attachment, field, True) assert getattr(attachment, field) try: setattr(attachment, field, 'non-boolean') assert False, "Should have raised an AttachmentException due to invalid data value" except AttachmentException as e: match = "Message: The %s field value must be of type bool Code: None" % field assert str(e) == match
def test_setter_validate_list_types(attachment_return): bugzilla = Bugsy() source = attachment_return['bugs']['1017315'][0] attachment = Attachment(bugzilla, **source) for field in ['bug_flags', 'flags']: setattr(attachment, field, ['flag']) assert getattr(attachment, field) == ['flag'] try: setattr(attachment, field, 'non-boolean') assert False, "Should have raised an AttachmentException due to invalid list value" except AttachmentException as e: match = "Message: The %s field value must be of type list Code: None" % field assert str(e) == match
def test_we_can_search_for_a_list_of_bug_numbers(): return_1 = { "bugs": [{ "component": "CSS Parsing and Computation", "product": "Core", "summary": "Map \"rebeccapurple\" to #663399 in named color list." }] } return_2 = { "bugs": [{ "component": "Marionette", "product": "Testing", "summary": "Marionette thinks that the play button in the music app is not displayed" }] } responses.add(responses.GET, rest_url('bug', 1017315), body=json.dumps(return_1), status=200, content_type='application/json', match_querystring=True) responses.add(responses.GET, rest_url('bug', 1017316), body=json.dumps(return_2), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bugs = bugzilla.search_for\ .bug_number(['1017315', '1017316'])\ .search() assert len(responses.calls) == 2 assert len(bugs) == 2 assert bugs[0].product == return_1['bugs'][0]['product'] assert bugs[0].summary == return_1['bugs'][0]['summary']
def test_we_can_search_whiteboard_fields(): whiteboard_return = { "bugs": [{ "component": "Marionette", "product": "Testing", "summary": "Tracking bug for uplifting is_displayed issue fix for WebDriver" }, { "component": "Marionette", "product": "Testing", "summary": "Marionette thinks that the play button in the music app is not displayed" }] } url_params = dict( assigned_to='*****@*****.**', whiteboard='affects', short_desc_type='allwordssubstr', ) responses.add(responses.GET, rest_url('bug', **url_params), body=json.dumps(whiteboard_return), status=200, content_type='application/json', match_querystring=True) bugzilla = Bugsy() bugs = bugzilla.search_for\ .assigned_to('*****@*****.**')\ .whiteboard("affects")\ .search() assert len(responses.calls) == 1 assert len(bugs) == 2 assert bugs[0].product == whiteboard_return['bugs'][0]['product'] assert bugs[0].summary == whiteboard_return['bugs'][0]['summary']
def wrappedpushbookmark(orig, pushop): result = orig(pushop) # pushop.ret was renamed to pushop.cgresult in Mercurial 3.2. We can drop # this branch once we drop <3.2 support. if hasattr(pushop, 'cgresult'): origresult = pushop.cgresult else: origresult = pushop.ret # Don't do anything if error from push. if not origresult: return result remoteurl = pushop.remote.url() tree = repository.resolve_uri_to_tree(remoteurl) # We don't support release trees (yet) because they have special flags # that need to get updated. if tree and tree in repository.RELEASE_TREES: return result ui = pushop.ui if tree and tree in ui.configlist('bzpost', 'excludetrees', default=[]): return result if tree: baseuri = repository.resolve_trees_to_uris([tree])[0][1].encode('utf-8') assert baseuri else: # This isn't a known Firefox tree. Fall back to resolving URLs by # hostname. # Only attend Mozilla's server. if not updateunknown(remoteurl, repository.BASE_WRITE_URI, ui): return result baseuri = remoteurl.replace(repository.BASE_WRITE_URI, repository.BASE_READ_URI).rstrip('/') bugsmap = {} lastbug = None lastnode = None for node in pushop.outgoing.missing: ctx = pushop.repo[node] # Don't do merge commits. if len(ctx.parents()) > 1: continue # Our bug parser is buggy for Gaia bump commit messages. if '<*****@*****.**>' in ctx.user(): continue # Pushing to Try (and possibly other repos) could push unrelated # changesets that have been pushed to an official tree but aren't yet # on this specific remote. We use the phase information as a proxy # for "already pushed" and prune public changesets from consideration. if tree == 'try' and ctx.phase() == phases.public: continue bugs = parse_bugs(ctx.description()) if not bugs: continue bugsmap.setdefault(bugs[0], []).append(ctx.hex()[0:12]) lastbug = bugs[0] lastnode = ctx.hex()[0:12] if not bugsmap: return result bzauth = getbugzillaauth(ui) if not bzauth: return result bzurl = ui.config('bugzilla', 'url', 'https://bugzilla.mozilla.org/rest') bugsy = Bugsy(username=bzauth.username, password=bzauth.password, userid=bzauth.userid, cookie=bzauth.cookie, api_key=bzauth.apikey, bugzilla_url=bzurl) def public_url_for_bug(bug): '''Turn 123 into "https://bugzilla.mozilla.org/show_bug.cgi?id=123".''' public_baseurl = bzurl.replace('rest', '').rstrip('/') return '%s/show_bug.cgi?id=%s' % (public_baseurl, bug) # If this is a try push, we paste the Treeherder link for the tip commit, because # the per-commit URLs don't have much value. # TODO roll this into normal pushing so we get a Treeherder link in bugs as well. if tree == 'try' and lastbug: treeherderurl = repository.treeherder_url(tree, lastnode) bug = bugsy.get(lastbug) comments = bug.get_comments() for comment in comments: if treeherderurl in comment.text: return result ui.write(_('recording Treeherder push at %s\n') % public_url_for_bug(lastbug)) bug.add_comment(treeherderurl) return result for bugnumber, nodes in bugsmap.items(): bug = bugsy.get(bugnumber) comments = bug.get_comments() missing_nodes = [] # When testing whether this changeset URL is referenced in a # comment, we only need to test for the node fragment. The # important side-effect is that each unique node for a changeset # is recorded in the bug. for node in nodes: if not any(node in comment.text for comment in comments): missing_nodes.append(node) if not missing_nodes: ui.write(_('bug %s already knows about pushed changesets\n') % bugnumber) continue lines = [] for node in missing_nodes: ctx = pushop.repo[node] lines.append('%s/rev/%s' % (baseuri, ctx.hex())) # description is using local encodings. Depending on the # configured encoding, replacement characters could be involved. We # use encoding.fromlocal() to get the raw bytes, which should be # valid UTF-8. lines.append(encoding.fromlocal(ctx.description()).splitlines()[0]) lines.append('') comment = '\n'.join(lines) ui.write(_('recording push at %s\n') % public_url_for_bug(bugnumber)) bug.add_comment(comment) return result