Projet

Général

Profil

Télécharger (4,62 ko) Statistiques
| Branche: | Tag: | Révision:

root / tests / test_admin_pages.py @ 80784d04

1
import os
2
import shutil
3
import StringIO
4
import time
5

    
6
try:
7
    import lasso
8
except ImportError:
9
    lasso = None
10

    
11
import pytest
12

    
13
from quixote import cleanup, get_publisher
14
from wcs.qommon import errors, sessions
15
from qommon.ident.password_accounts import PasswordAccount
16
from wcs.qommon.http_request import HTTPRequest
17
from wcs.qommon.template import get_current_theme
18
from wcs.categories import Category
19
from wcs.roles import Role
20
from wcs.workflows import Workflow
21
from wcs.formdef import FormDef
22
from wcs import fields
23

    
24
from utilities import get_app, login, create_temporary_pub
25

    
26
def setup_module(module):
27
    cleanup()
28

    
29
    global pub
30

    
31
    pub = create_temporary_pub()
32

    
33
    req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
34
    pub.set_app_dir(req)
35
    pub.cfg['identification'] = {'methods': ['password']}
36
    pub.write_cfg()
37

    
38
def create_superuser():
39
    global user1
40
    if pub.user_class.has_key('admin'):
41
        user1 = pub.user_class.get('admin')
42
        user1.is_admin = True
43
        user1.roles = []
44
        return
45
    user1 = pub.user_class(name='admin')
46
    user1.id = 'admin'
47
    user1.is_admin = True
48
    user1.roles = []
49
    user1.store()
50

    
51
    account1 = PasswordAccount(id='admin')
52
    account1.set_password('admin')
53
    account1.user_id = user1.id
54
    account1.store()
55

    
56
    pub.cfg['identification'] = {'methods': ['password']}
57
    pub.write_cfg()
58

    
59
def create_role():
60
    Role.wipe()
61
    role = Role(name='foobar')
62
    role.store()
63
    return role
64

    
65
def teardown_module(module):
66
    shutil.rmtree(pub.APP_DIR)
67

    
68
@pytest.fixture
69
def empty_siteoptions():
70
    open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w').close()
71

    
72
def test_with_superuser():
73
    create_superuser()
74
    app = login(get_app(pub))
75
    resp = app.get('/backoffice/')
76
    # this makes sure the extension loaded properly
77
    assert '<span id="applabel">Publik</span>' in resp.body
78

    
79
def test_general_admin_permissions():
80
    create_superuser()
81
    app = login(get_app(pub))
82
    resp = app.get('/backoffice/settings/', status=200)
83
    pub.cfg['admin-permissions'] = {'settings': ['XXX']}
84
    pub.write_cfg()
85
    resp = app.get('/backoffice/settings/', status=403)
86
    user1.roles = ['XXX']
87
    user1.store()
88
    resp = app.get('/backoffice/settings/', status=200)
89
    del pub.cfg['admin-permissions']
90
    pub.write_cfg()
91

    
92
def test_aq_permissions_panel(empty_siteoptions):
93
    create_superuser()
94
    app = login(get_app(pub))
95
    resp = app.get('/backoffice/settings/')
96
    assert not 'aq/permissions' in resp.body
97

    
98
    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
99
        if not pub.site_options.has_section('options'):
100
            pub.site_options.add_section('options')
101
        pub.site_options.set('options', 'auquotidien-links', 'true')
102
        pub.site_options.write(fd)
103

    
104
    resp = app.get('/backoffice/settings/')
105
    assert 'aq/permissions' in resp.body
106
    resp = app.get('/backoffice/settings/aq/permissions')
107

    
108
def test_menu_items(empty_siteoptions):
109
    create_superuser()
110
    role = create_role()
111

    
112
    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
113
        if not pub.site_options.has_section('options'):
114
            pub.site_options.add_section('options')
115
        pub.site_options.set('options', 'auquotidien-links', 'true')
116
        pub.site_options.write(fd)
117

    
118
    for area in ('links', 'announces', 'events', 'links', 'payments'):
119

    
120
        with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
121
            if not pub.site_options.has_section('options'):
122
                pub.site_options.add_section('options')
123
            pub.site_options.set('options', 'auquotidien-%s' % area, 'true')
124
            pub.site_options.write(fd)
125

    
126
        pub.cfg['aq-permissions'] = {area: None}
127
        pub.write_cfg()
128

    
129
        user1.is_admin = True
130
        user1.roles = []
131
        user1.store()
132

    
133
        app = login(get_app(pub))
134
        resp = app.get('/backoffice/')
135
        assert not '/%s/' % area in resp.body
136
        resp = app.get('/backoffice/%s/' % area, status=403)
137

    
138
        pub.cfg['aq-permissions'] = {area: 'XXX'}
139
        pub.write_cfg()
140

    
141
        resp = app.get('/backoffice/')
142
        assert '/%s/' % area in resp.body
143
        resp = app.get('/backoffice/%s/' % area, status=200)
144

    
145
        user1.is_admin = False
146
        user1.roles = [role.id]
147
        user1.store()
148
        resp = app.get('/backoffice/')
149
        assert not '/%s/' % area in resp.body
150
        resp = app.get('/backoffice/%s/' % area, status=403)
151

    
152
        user1.is_admin = False
153
        user1.roles = [role.id, 'XXX']
154
        user1.store()
155
        resp = app.get('/backoffice/')
156
        assert '/%s/' % area in resp.body
157
        resp = app.get('/backoffice/%s/' % area, status=200)
(1-1/4)