'Test to add an email belonging to another user.'

@patch('mozillians.common.authbackend.messages.error')
def test_add_email_belonging_to_other_user(self, mocked_message):

    user1 = UserFactory.create(email='foo@example.com')
    UserFactory.create(email='bar@example.com')
    request_mock = Mock(spec=HttpRequest)
    request_mock.user = user1
    request_mock.user.is_authenticated = Mock()
    request_mock.user.is_authenticated.return_value = True
    self.backend.request = request_mock
    claims = {'email': 'bar@example.com'}
    returned_user = self.backend.filter_users_by_claims(claims)
    email_q = ExternalAccount.objects.filter(type=ExternalAccount.TYPE_EMAIL, user=user1.userprofile, identifier='bar@example.com')
    mocked_message.assert_called_once_with(request_mock, u'Email    bar@example.com    already    exists    in    the    database.')
    ok_((not email_q.exists()))
    eq_(len(returned_user), 1)
    eq_(returned_user[0], user1)
