1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the webservice marshallers."""
9
from zope.component import getUtility
11
from canonical.launchpad.testing.pages import (
12
LaunchpadWebServiceCaller,
13
webservice_for_person,
15
from canonical.launchpad.webapp.interfaces import IPlacelessAuthUtility
16
from canonical.launchpad.webapp.servers import WebServiceTestRequest
17
from canonical.testing.layers import DatabaseFunctionalLayer
18
from lp.app.webservice.marshallers import TextFieldMarshaller
19
from lp.testing import logout, TestCaseWithFactory
23
url = "/bugs/%d" % bug.id
27
class TestTextFieldMarshaller(TestCaseWithFactory):
29
layer = DatabaseFunctionalLayer
31
def _makeRequest(self, is_anonymous):
32
"""Create either an anonymous or authenticated request."""
33
request = WebServiceTestRequest()
36
getUtility(IPlacelessAuthUtility).unauthenticatedPrincipal())
38
request.setPrincipal(self.factory.makePerson())
41
def test_unmarshall_obfuscated(self):
42
# Data is obfuccated if the request is anonynous.
43
request = self._makeRequest(is_anonymous=True)
44
marshaller = TextFieldMarshaller(None, request)
45
result = marshaller.unmarshall(None, u"foo@example.com")
46
self.assertEqual(u"<email address hidden>", result)
48
def test_unmarshall_not_obfuscated(self):
49
# Data is not obfuccated if the request is authenticated.
50
request = self._makeRequest(is_anonymous=False)
51
marshaller = TextFieldMarshaller(None, request)
52
result = marshaller.unmarshall(None, u"foo@example.com")
53
self.assertEqual(u"foo@example.com", result)
56
class TestWebServiceObfuscation(TestCaseWithFactory):
57
"""Integration test for obfuscation marshaller.
59
Not using WebServiceTestCase because that assumes too much about users
62
layer = DatabaseFunctionalLayer
64
email_address = "joe@example.com"
65
email_address_obfuscated = "<email address hidden>"
66
email_address_obfuscated_escaped = "<email address hidden>"
67
bug_title = "Title with address %s in it"
68
bug_description = "Description with address %s in it"
71
"""Create a bug with an email address in title and description."""
72
bug = self.factory.makeBug(
73
title=self.bug_title % self.email_address,
74
description=self.bug_description % self.email_address)
78
def test_email_address_obfuscated(self):
79
# Email addresses are obfuscated for anonymous users.
82
webservice = LaunchpadWebServiceCaller()
83
result = webservice(ws_url(bug)).jsonBody()
85
self.bug_title % self.email_address_obfuscated,
88
self.bug_description % self.email_address_obfuscated,
89
result['description'])
91
def test_email_address_not_obfuscated(self):
92
# Email addresses are not obfuscated for authenticated users.
94
user = self.factory.makePerson()
95
webservice = webservice_for_person(user)
96
result = webservice(ws_url(bug)).jsonBody()
97
self.assertEqual(self.bug_title % self.email_address, result['title'])
99
self.bug_description % self.email_address, result['description'])
101
def test_xhtml_email_address_not_obfuscated(self):
102
# Email addresses are not obfuscated for authenticated users.
103
bug = self._makeBug()
104
user = self.factory.makePerson()
105
webservice = webservice_for_person(user)
107
ws_url(bug), headers={'Accept': 'application/xhtml+xml'})
108
self.assertIn(self.email_address, result.body)
110
self.email_address_obfuscated_escaped, result.body)
112
def test_xhtml_email_address_obfuscated(self):
113
# Email addresses are obfuscated in the XML representation for
115
bug = self._makeBug()
117
webservice = LaunchpadWebServiceCaller()
119
ws_url(bug), headers={'Accept': 'application/xhtml+xml'})
120
self.assertNotIn(self.email_address, result.body)
121
self.assertIn(self.email_address_obfuscated_escaped, result.body)
123
def test_etags_differ_for_anon_and_non_anon_represetations(self):
124
# When a webservice client retrieves data anonymously, this
125
# data should not be used in later write requests, if the
126
# text fields contain obfuscated email addresses. The etag
127
# for a GET request is calculated after the email address
128
# obfuscation and thus differs from the etag returned for
129
# not obfuscated data, so clients usings etags to check if the
130
# cached data is up to date will not use the obfuscated data
131
# in PATCH or PUT requests.
132
bug = self._makeBug()
133
user = self.factory.makePerson()
134
webservice = webservice_for_person(user)
135
etag_logged_in = webservice(ws_url(bug)).getheader('etag')
137
webservice = LaunchpadWebServiceCaller()
138
etag_logged_out = webservice(ws_url(bug)).getheader('etag')
139
self.assertNotEqual(etag_logged_in, etag_logged_out)