Exemplo n.º 1
0
 def test_newWebhook_permissions(self):
     self.useFixture(FeatureFixture({'webhooks.new.enabled': 'true'}))
     webservice = LaunchpadWebServiceCaller()
     response = webservice.named_post(self.target_url,
                                      'newWebhook',
                                      delivery_url='http://example.com/ep',
                                      event_types=[self.event_type],
                                      api_version='devel')
     self.assertEqual(401, response.status)
     self.assertIn('launchpad.Edit', response.body)
class TestQuestionRepresentation(TestCaseWithFactory):
    """Test ways of interacting with Question webservice representations."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestQuestionRepresentation, self).setUp()
        with celebrity_logged_in('admin'):
            self.question = self.factory.makeQuestion(
                title="This is a question")
            self.target_name = self.question.target.name

        self.webservice = LaunchpadWebServiceCaller('launchpad-library',
                                                    'salgado-change-anything')
        self.webservice.default_api_version = 'devel'

    def findQuestionTitle(self, response):
        """Find the question title field in an XHTML document fragment."""
        soup = BeautifulSoup(response.body)
        dt = soup.find('dt', text="title")
        dd = dt.findNextSibling('dd')
        return str(dd.contents.pop())

    def test_top_level_question_get(self):
        # The top level question set can be used via the api to get
        # a question by id via redirect without url hacking.
        response = self.webservice.get('/questions/%s' % self.question.id,
                                       'application/xhtml+xml')
        self.assertEqual(response.status, 200)

    def test_GET_xhtml_representation(self):
        # A question's xhtml representation is available on the api.
        response = self.webservice.get(
            '/%s/+question/%d' % (self.target_name, self.question.id),
            'application/xhtml+xml')
        self.assertEqual(response.status, 200)

        self.assertEqual(self.findQuestionTitle(response),
                         "<p>This is a question</p>")

    def test_PATCH_xhtml_representation(self):
        # You can update the question through the api with PATCH.
        new_title = "No, this is a question"

        question_json = self.webservice.get(
            '/%s/+question/%d' %
            (self.target_name, self.question.id)).jsonBody()

        response = self.webservice.patch(
            question_json['self_link'],
            'application/json',
            dumps(dict(title=new_title)),
            headers=dict(accept='application/xhtml+xml'))

        self.assertEqual(response.status, 209)

        self.assertEqual(self.findQuestionTitle(response),
                         "<p>No, this is a question</p>")

    def test_reject(self):
        # A question can be rejected via the API.
        question_url = '/%s/+question/%d' % (self.target_name,
                                             self.question.id)
        response = self.webservice.named_post(question_url,
                                              'reject',
                                              comment='A rejection message')
        self.assertEqual(201, response.status)
        self.assertThat(response.getheader('location'),
                        EndsWith('%s/messages/1' % question_url))
        self.assertEqual(QuestionStatus.INVALID, self.question.status)

    def test_reject_not_answer_contact(self):
        # If the requesting user is not an answer contact, the API returns a
        # suitable error.
        with celebrity_logged_in('admin'):
            random_person = self.factory.makePerson()
        webservice = webservice_for_person(
            random_person, permission=OAuthPermission.WRITE_PUBLIC)
        webservice.default_api_version = 'devel'
        response = webservice.named_post('/%s/+question/%d' %
                                         (self.target_name, self.question.id),
                                         'reject',
                                         comment='A rejection message')
        self.assertEqual(401, response.status)