~lucio.torre/ubuntuone-client/windows-port

« back to all changes in this revision

Viewing changes to tests/oauthdesktop/test_key_acls.py

  • Committer: Rodney Dawes
  • Date: 2009-05-12 13:36:05 UTC
  • Revision ID: rodney.dawes@canonical.com-20090512133605-6aqs6e8xnnmp5u1p
        Import the code
        Hook up lint/trial tests in setup.py
        Use icontool now instead of including the render script
        Add missing python-gnome2-desktop to package dependencies
        Update debian/rules to fix the icon cache issue

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# test_key_acls - tests for canonical.ubuntuone.oauthdesktop.key_acls
 
2
#
 
3
# Author: Stuart Langridge <stuart.langridge@canonical.com>
 
4
#
 
5
# Copyright 2009 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
"""Tests for the ACL setting code for ubuntuone-oauth-login."""
 
19
 
 
20
import os, StringIO
 
21
import xdg.BaseDirectory
 
22
from contrib.mocker import MockerTestCase, IN, ANY
 
23
from canonical.ubuntuone.oauthdesktop.key_acls import (
 
24
  get_privileged_config_folder, get_acl_preregistrations, set_all_key_acls,
 
25
  set_single_acl)
 
26
 
 
27
class Loader(MockerTestCase):
 
28
    """Confirm only the /etc/xdg config files are loaded"""
 
29
 
 
30
    def test_etc_found(self):
 
31
        """Is the /etc/xdg folder found if present?"""
 
32
        
 
33
        ETC_FOLDER = "/etc/xdg/ubuntuone"
 
34
        osp = self.mocker.replace("os.path")
 
35
        osp.exists(ETC_FOLDER)
 
36
        self.mocker.result(True)
 
37
        self.mocker.replay()
 
38
        
 
39
        self.assertEqual(ETC_FOLDER,
 
40
          get_privileged_config_folder(use_source_tree_folder=False))
 
41
 
 
42
    def test_etc_found_despite_home(self):
 
43
        """Is the /etc/xdg folder found even if the $HOME folder is present?"""
 
44
        
 
45
        ETC_FOLDER = "/etc/xdg/ubuntuone"
 
46
        HOME_FOLDER = os.path.join(xdg.BaseDirectory.xdg_config_home, 
 
47
          "ubuntuone")
 
48
        osp = self.mocker.replace("os.path")
 
49
        osp.exists(ETC_FOLDER)
 
50
        self.mocker.result(True)
 
51
        osp.exists(HOME_FOLDER)
 
52
        self.mocker.result(True)
 
53
        self.mocker.replay()
 
54
        
 
55
        self.assertEqual(ETC_FOLDER,
 
56
          get_privileged_config_folder(use_source_tree_folder=False))
 
57
 
 
58
    def test_etc_files_found(self):
 
59
        """Are files in /etc found?"""
 
60
        ETC_FOLDER = "/etc/xdg/ubuntuone"
 
61
        ETC_FILES_FOLDER = "/etc/xdg/ubuntuone/oauth_registration.d"
 
62
        FILE_LIST = ["a", "b"]
 
63
        osp = self.mocker.replace("os.path")
 
64
        osp.exists(ETC_FOLDER)
 
65
        self.mocker.result(True)
 
66
        osp.isdir(ETC_FILES_FOLDER)
 
67
        self.mocker.result(True)
 
68
        listdir = self.mocker.replace("os.listdir")
 
69
        listdir(ETC_FILES_FOLDER)
 
70
        self.mocker.result(FILE_LIST)
 
71
        self.mocker.replay()
 
72
        
 
73
        our_list = sorted(
 
74
          [os.path.join(ETC_FILES_FOLDER, x) for x in FILE_LIST])
 
75
        their_list = sorted(
 
76
          get_acl_preregistrations(use_source_tree_folder=False))
 
77
        self.assertEqual(our_list, their_list)
 
78
        
 
79
    def test_set_single_acl(self):
 
80
        """Does set_single_acl work?"""
 
81
 
 
82
        giifr = self.mocker.replace(
 
83
          "canonical.ubuntuone.oauthdesktop.key_acls.get_item_ids_for_realm")
 
84
        igas = self.mocker.replace("gnomekeyring.item_get_acl_sync")
 
85
        isas = self.mocker.replace("gnomekeyring.item_set_acl_sync")
 
86
        acon = self.mocker.replace("gnomekeyring.AccessControl")
 
87
        self.expect(giifr("realm", "consumer_key")).result([999])
 
88
        self.expect(igas(None, 999)).result([])
 
89
        ac1 = self.mocker.mock()
 
90
        self.expect(acon(ANY, ANY)).result(ac1)
 
91
        ac1.set_display_name("application_name")
 
92
        ac1.set_path_name("/tmp/exe_path")
 
93
        self.expect(isas(None, 999, [ac1])).result(None)
 
94
        self.expect(giifr("realm2", "consumer_key2")).result([9999])
 
95
        self.expect(igas(None, 9999)).result([])
 
96
        ac2 = self.mocker.mock()
 
97
        self.expect(acon(ANY, ANY)).result(ac2)
 
98
        ac2.set_display_name("application_name2")
 
99
        ac2.set_path_name("/tmp/exe_path2")
 
100
        self.expect(isas(None, 9999, [ac2])).result(None)
 
101
        self.mocker.replay()
 
102
 
 
103
        set_single_acl([
 
104
          ("realm", "consumer_key", "/tmp/exe_path", "application_name"),
 
105
          ("realm2", "consumer_key2", "/tmp/exe_path2", "application_name2"),
 
106
        ])
 
107
        
 
108
        
 
109
    def test_etc_files_parsed(self):
 
110
        """Are files in /etc parsed correctly?"""
 
111
        
 
112
        ETC_FOLDER = "/etc/xdg/ubuntuone"
 
113
        ETC_FILES_FOLDER = "/etc/xdg/ubuntuone/oauth_registration.d"
 
114
        FILE_LIST = ["a", "b"]
 
115
        osp = self.mocker.replace("os.path")
 
116
        osp.exists(ETC_FOLDER)
 
117
        self.mocker.result(True)
 
118
        osp.isdir(ETC_FILES_FOLDER)
 
119
        self.mocker.result(True)
 
120
        listdir = self.mocker.replace("os.listdir")
 
121
        listdir(ETC_FILES_FOLDER)
 
122
        self.mocker.result(FILE_LIST)
 
123
 
 
124
        sio1 = StringIO.StringIO("""[app1]
 
125
realm = https://realm.example.com/
 
126
consumer_key = example_key
 
127
exe_path = /nowhere/executable/path
 
128
application_name = example_app_name
 
129
 
 
130
[app2]
 
131
realm = https://other.example.com/
 
132
consumer_key = example_key
 
133
exe_path = /nowhere/executable/path
 
134
application_name = example_app_name
 
135
 
 
136
""")
 
137
        sio2 = StringIO.StringIO("""[app3]
 
138
exe_path = /nowhere/path/2
 
139
application_name = example_app_name2
 
140
consumer_key = example_key2
 
141
realm = https://realm2.example.com/
 
142
""")
 
143
        mock_open = self.mocker.replace(open)
 
144
        mock_open(os.path.join(ETC_FILES_FOLDER, "a"))
 
145
        self.mocker.result(sio1)
 
146
        mock_open(os.path.join(ETC_FILES_FOLDER, "b"))
 
147
        self.mocker.result(sio2)
 
148
        
 
149
        ssa = self.mocker.replace(
 
150
          "canonical.ubuntuone.oauthdesktop.key_acls.set_single_acl")
 
151
        # list may come up in any order
 
152
        ssa(IN([
 
153
              [
 
154
                ("https://realm.example.com/", "example_key", 
 
155
                "/nowhere/executable/path", "example_app_name"),
 
156
                ("https://other.example.com/", "example_key", 
 
157
                "/nowhere/executable/path", "example_app_name"),
 
158
              ],
 
159
              [
 
160
                ("https://other.example.com/", "example_key", 
 
161
                "/nowhere/executable/path", "example_app_name"),
 
162
                ("https://realm.example.com/", "example_key", 
 
163
                "/nowhere/executable/path", "example_app_name"),
 
164
              ],
 
165
            ]), specific_item_id=None
 
166
        )
 
167
        self.mocker.result(None)
 
168
        ssa([
 
169
            ("https://realm2.example.com/", "example_key2", 
 
170
            "/nowhere/path/2", "example_app_name2")
 
171
            ], specific_item_id=None)
 
172
        self.mocker.result(None)
 
173
        self.mocker.replay()
 
174
        
 
175
        set_all_key_acls(use_source_tree_folder=False)
 
176
    
 
177
    
 
178
    
 
179
    
 
180
    
 
181