<filename>tests/test_bugsy.py
import bugsy
from . import rest_url
from bugsy import Bugsy, BugsyException, LoginException
from bugsy import Bug

import responses
import json

example_return = {u'faults': [], u'bugs': [{u'cf_tracking_firefox29': u'---', u'classification': u'Other', u'creator': u'<EMAIL>', u'cf_status_firefox30':
u'---', u'depends_on': [], u'cf_status_firefox32': u'---', u'creation_time': u'2014-05-28T23:57:58Z', u'product': u'Release Engineering', u'cf_user_story': u'', u'dupe_of': None, u'cf_tracking_firefox_relnote': u'---', u'keywords': [], u'cf_tracking_b2g18': u'---', u'summary': u'Schedule Mn tests on o\
pt Linux builds on cedar', u'id': 1017315, u'assigned_to_detail': {u'id': 347295, u'email': u'<EMAIL>', u'name': u'<EMAIL>',
u'real_name': u'<NAME> (:jgriffin)'}, u'severity': u'normal', u'is_confirmed': True, u'is_creator_accessible': True, u'cf_status_b2g_1_1_hd':
 u'---', u'qa_contact_detail': {u'id': 20203, u'email': u'<EMAIL>', u'name': u'<EMAIL>', u'real_name': u'<NAME> [:catlee]'},
 u'priority': u'--', u'platform': u'All', u'cf_crash_signature': u'', u'version': u'unspecified', u'cf_qa_whiteboard': u'', u'cf_status_b2g_1_3t': u'--\
-', u'cf_status_firefox31': u'---', u'is_open': False, u'cf_blocking_fx': u'---', u'status': u'RESOLVED', u'cf_tracking_relnote_b2g': u'---', u'cf_stat\
us_firefox29': u'---', u'blocks': [], u'qa_contact': u'<EMAIL>', u'see_also': [], u'component': u'General Automation', u'cf_tracking_firefox\
32': u'---', u'cf_tracking_firefox31': u'---', u'cf_tracking_firefox30': u'---', u'op_sys': u'All', u'groups': [], u'cf_blocking_b2g': u'---', u'target\
_milestone': u'---', u'is_cc_accessible': True, u'cf_tracking_firefox_esr24': u'---', u'cf_status_b2g_1_2': u'---', u'cf_status_b2g_1_3': u'---', u'cf_\
status_b2g18': u'---', u'cf_status_b2g_1_4': u'---', u'url': u'', u'creator_detail': {u'id': 347295, u'email': u'<EMAIL>', u'name': u'<EMAIL>\
<EMAIL>', u'real_name': u'<NAME> (:jgriffin)'}, u'whiteboard': u'', u'cf_status_b2g_2_0': u'---', u'cc_detail': [{u'id': 30066, u'em\
ail': u'<EMAIL>', u'name': u'<EMAIL>', u'real_name': u'<NAME> [:coop]'}, {u'id': 397261, u'email': u'<EMAIL>', u'nam\
e': u'<EMAIL>', u'real_name': u'<NAME> :automatedtester'}, {u'id': 438921, u'email': u'<EMAIL>', u'name': u'<EMAIL> ', u'real_name': u'<NAME> (:jlund)'}, {u'id': 418814, u'email': u'<EMAIL>', u'name': u'<EMAIL>', u'real_name': u'<NAME> [:md\
as]'}], u'alias': None, u'cf_tracking_b2g_v1_2': u'---', u'cf_tracking_b2g_v1_3': u'---', u'flags': [], u'assigned_to': u'<EMAIL>', u'cf_s\
tatus_firefox_esr24': u'---', u'resolution': u'FIXED', u'last_change_time': u'2014-05-30T21:20:17Z', u'cc': [u'<EMAIL>', u'<EMAIL>'
, u'<EMAIL>', u'<EMAIL>'], u'cf_blocking_fennec': u'---'}]}

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"

@responses.activate
def test_we_get_a_login_exception_when_details_are_wrong():
    responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                      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."

@responses.activate
def test_we_cant_post_without_passing_a_bug_object():
    responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                      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"

@responses.activate
def test_we_can_get_a_bug():
    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)
    assert bug.id == 1017315
    assert bug.status == 'RESOLVED'
    assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'

@responses.activate
def test_we_can_get_a_bug_with_login_token():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                        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)
  assert bug.id == 1017315
  assert bug.status == 'RESOLVED'
  assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'

@responses.activate
def test_we_can_get_username_with_userid_cookie():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/user/1234?token=<PASSWORD>',
                        body='{"users": [{"name": "<EMAIL>"}]}', status=200,
                        content_type='application/json', match_querystring=True)

  bugzilla = Bugsy(userid='1234', cookie='abcd')
  assert bugzilla.username == '<EMAIL>'

@responses.activate
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=<PASSWORD>',
                      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

@responses.activate
def test_we_can_put_a_current_bug():
    responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                      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')
    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'

@responses.activate
def test_we_handle_errors_from_bugzilla_when_posting():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                    body='{"token": "<PASSWORD>"}', 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."

@responses.activate
def test_we_handle_errors_from_bugzilla_when_updating_a_bug():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                    body='{"token": "<PASSWORD>"}', 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=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."

@responses.activate
def test_we_can_set_the_user_agent_to_bugsy():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                    body='{"token": "foobar"}', status=200,
                    content_type='application/json', match_querystring=True)
  Bugsy("foo", "bar")
  assert responses.calls[0].request.headers['User-Agent'] == "Bugsy"

<filename>tests/test_bugsy.py
import bugsy
from . import rest_url
from bugsy import Bugsy, BugsyException, LoginException
from bugsy import Bug

import responses
import json

example_return = {u'faults': [], u'bugs': [{u'cf_tracking_firefox29': u'---', u'classification': u'Other', u'creator': u'<EMAIL>', u'cf_status_firefox30':
u'---', u'depends_on': [], u'cf_status_firefox32': u'---', u'creation_time': u'2014-05-28T23:57:58Z', u'product': u'Release Engineering', u'cf_user_story': u'', u'dupe_of': None, u'cf_tracking_firefox_relnote': u'---', u'keywords': [], u'cf_tracking_b2g18': u'---', u'summary': u'Schedule Mn tests on o\
pt Linux builds on cedar', u'id': 1017315, u'assigned_to_detail': {u'id': 347295, u'email': u'<EMAIL>', u'name': u'<EMAIL>',
u'real_name': u'<NAME> (:jgriffin)'}, u'severity': u'normal', u'is_confirmed': True, u'is_creator_accessible': True, u'cf_status_b2g_1_1_hd':
 u'---', u'qa_contact_detail': {u'id': 20203, u'email': u'<EMAIL>', u'name': u'<EMAIL>', u'real_name': u'<NAME> [:catlee]'},
 u'priority': u'--', u'platform': u'All', u'cf_crash_signature': u'', u'version': u'unspecified', u'cf_qa_whiteboard': u'', u'cf_status_b2g_1_3t': u'--\
-', u'cf_status_firefox31': u'---', u'is_open': False, u'cf_blocking_fx': u'---', u'status': u'RESOLVED', u'cf_tracking_relnote_b2g': u'---', u'cf_stat\
us_firefox29': u'---', u'blocks': [], u'qa_contact': u'<EMAIL>', u'see_also': [], u'component': u'General Automation', u'cf_tracking_firefox\
32': u'---', u'cf_tracking_firefox31': u'---', u'cf_tracking_firefox30': u'---', u'op_sys': u'All', u'groups': [], u'cf_blocking_b2g': u'---', u'target\
_milestone': u'---', u'is_cc_accessible': True, u'cf_tracking_firefox_esr24': u'---', u'cf_status_b2g_1_2': u'---', u'cf_status_b2g_1_3': u'---', u'cf_\
status_b2g18': u'---', u'cf_status_b2g_1_4': u'---', u'url': u'', u'creator_detail': {u'id': 347295, u'email': u'<EMAIL>', u'name': u'<EMAIL>\
<EMAIL>', u'real_name': u'<NAME> (:jgriffin)'}, u'whiteboard': u'', u'cf_status_b2g_2_0': u'---', u'cc_detail': [{u'id': 30066, u'em\
ail': u'<EMAIL>', u'name': u'<EMAIL>', u'real_name': u'<NAME> [:coop]'}, {u'id': 397261, u'email': u'<EMAIL>', u'nam\
e': u'<EMAIL>', u'real_name': u'<NAME> :automatedtester'}, {u'id': 438921, u'email': u'<EMAIL>', u'name': u'<EMAIL> ', u'real_name': u'<NAME> (:jlund)'}, {u'id': 418814, u'email': u'<EMAIL>', u'name': u'<EMAIL>', u'real_name': u'<NAME> [:md\
as]'}], u'alias': None, u'cf_tracking_b2g_v1_2': u'---', u'cf_tracking_b2g_v1_3': u'---', u'flags': [], u'assigned_to': u'<EMAIL>', u'cf_s\
tatus_firefox_esr24': u'---', u'resolution': u'FIXED', u'last_change_time': u'2014-05-30T21:20:17Z', u'cc': [u'<EMAIL>', u'<EMAIL>'
, u'<EMAIL>', u'<EMAIL>'], u'cf_blocking_fennec': u'---'}]}

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"

@responses.activate
def test_we_get_a_login_exception_when_details_are_wrong():
    responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                      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."

@responses.activate
def test_we_cant_post_without_passing_a_bug_object():
    responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                      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"

@responses.activate
def test_we_can_get_a_bug():
    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)
    assert bug.id == 1017315
    assert bug.status == 'RESOLVED'
    assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'

@responses.activate
def test_we_can_get_a_bug_with_login_token():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                        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)
  assert bug.id == 1017315
  assert bug.status == 'RESOLVED'
  assert bug.summary == 'Schedule Mn tests on opt Linux builds on cedar'

@responses.activate
def test_we_can_get_username_with_userid_cookie():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/user/1234?token=<PASSWORD>',
                        body='{"users": [{"name": "<EMAIL>"}]}', status=200,
                        content_type='application/json', match_querystring=True)

  bugzilla = Bugsy(userid='1234', cookie='abcd')
  assert bugzilla.username == '<EMAIL>'

@responses.activate
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=<PASSWORD>',
                      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

@responses.activate
def test_we_can_put_a_current_bug():
    responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                      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')
    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'

@responses.activate
def test_we_handle_errors_from_bugzilla_when_posting():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                    body='{"token": "<PASSWORD>"}', 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."

@responses.activate
def test_we_handle_errors_from_bugzilla_when_updating_a_bug():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                    body='{"token": "<PASSWORD>"}', 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=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."

@responses.activate
def test_we_can_set_the_user_agent_to_bugsy():
  responses.add(responses.GET, 'https://bugzilla.mozilla.org/rest/login?login=foo&password=<PASSWORD>',
                    body='{"token": "foobar"}', status=200,
                    content_type='application/json', match_querystring=True)
  Bugsy("foo", "bar")
  assert responses.calls[0].request.headers['User-Agent'] == "Bugsy"
