~leonardr/launchpadlib/trusted-workflow-tests-2

« back to all changes in this revision

Viewing changes to src/launchpadlib/testing/helpers.py

  • Committer: Leonard Richardson
  • Date: 2009-10-29 11:17:16 UTC
  • mfrom: (67.1.11 trusted-workflow)
  • Revision ID: leonard.richardson@canonical.com-20091029111716-22ey223qpjvo7obv
[r=Edwin] Introduced the framework for a trusted client.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
 
31
31
from launchpadlib.launchpad import Launchpad
 
32
from launchpadlib.credentials import (
 
33
    AuthorizeRequestTokenProcess, Credentials)
32
34
 
33
35
 
34
36
class TestableLaunchpad(Launchpad):
61
63
salgado_with_full_permissions = KnownTokens('salgado-change-anything', 'test')
62
64
salgado_read_nonprivate = KnownTokens('salgado-read-nonprivate', 'secret')
63
65
nopriv_read_nonprivate = KnownTokens('nopriv-read-nonprivate', 'mystery')
 
66
 
 
67
 
 
68
class ScriptableRequestTokenAuthorization(AuthorizeRequestTokenProcess):
 
69
    """A request token process that doesn't need any user input.
 
70
 
 
71
    The AuthorizeRequestTokenProcess is supposed to be hooked up to a
 
72
    user interface, but that makes it difficult to test. This subclass
 
73
    is designed to be easy to test.
 
74
    """
 
75
 
 
76
    def __init__(self, consumer_name, username, password, choose_access_level,
 
77
                 allow_access_levels=[], max_failed_attempts=2,
 
78
                 web_root="http://launchpad.dev:8085/"):
 
79
 
 
80
        # Get a request token.
 
81
        self.credentials = Credentials(consumer_name)
 
82
        self.credentials.get_request_token(web_root=web_root)
 
83
 
 
84
        # Initialize the superclass with the new request token.
 
85
        super(ScriptableRequestTokenAuthorization, self).__init__(
 
86
            web_root, consumer_name, self.credentials._request_token.key,
 
87
                allow_access_levels, max_failed_attempts)
 
88
 
 
89
        self.username = username
 
90
        self.password = password
 
91
        self.choose_access_level = choose_access_level
 
92
 
 
93
    def __call__(self):
 
94
        super(ScriptableRequestTokenAuthorization, self).__call__()
 
95
 
 
96
        # Now verify that it worked by exchanging the authorized
 
97
        # request token for an access token.
 
98
        if self.choose_access_level != self.UNAUTHORIZED_ACCESS_LEVEL:
 
99
            self.credentials.exchange_request_token_for_access_token(
 
100
                web_root=self.web_root)
 
101
            return self.credentials.access_token
 
102
        return None
 
103
 
 
104
    def open_login_page_in_user_browser(self, url):
 
105
        """Print a status message."""
 
106
        print ("[If this were a real application, the end-user's web "
 
107
               "browser would be opened to %s]" % url)
 
108
 
 
109
    def setup(self, suggested_message):
 
110
        print suggested_message
 
111
 
 
112
    def input_username(self, cached_username, suggested_message):
 
113
        """Collect the Launchpad username from the end-user."""
 
114
        print suggested_message
 
115
        if cached_username is not None:
 
116
            print "Cached username: " + cached_username
 
117
        return self.username
 
118
 
 
119
    def input_password(self, suggested_message):
 
120
        """Collect the Launchpad password from the end-user."""
 
121
        print suggested_message
 
122
        return self.password
 
123
 
 
124
    def input_access_level(self, available_levels, suggested_message):
 
125
        """Collect the desired level of access from the end-user."""
 
126
        print suggested_message
 
127
        print [level['value'] for level in available_levels]
 
128
        return self.choose_access_level
 
129
 
 
130
    def authentication_failure(self, suggested_message):
 
131
        """The user entered invalid credentials."""
 
132
        print suggested_message
 
133
 
 
134
    def user_refused_to_authorize(self, suggested_message):
 
135
        """The user refused to authorize a request token."""
 
136
        print suggested_message
 
137
 
 
138
    def user_authorized(self, access_level, suggested_message):
 
139
        """The user authorized a request token with some access level."""
 
140
        print suggested_message
 
141
 
 
142
    def server_consumer_differs_from_client_consumer(
 
143
        self, client_name, real_name, suggested_message):
 
144
        """The client seems to be lying or mistaken about its name."""
 
145
        print suggested_message
 
146
 
 
147
    def success(self, suggested_message):
 
148
        """The token was successfully authorized."""
 
149
        print suggested_message
 
150
 
 
151