Projet

Général

Profil

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

root / tests / test_user_pages.py @ 9e7f79a9

1
import os
2
import shutil
3
import time
4

    
5
import pytest
6

    
7
from quixote import cleanup, get_publisher
8
from wcs.qommon import errors, sessions
9
from wcs.qommon.ident.password_accounts import PasswordAccount
10
from wcs.qommon.http_request import HTTPRequest
11
from wcs.categories import Category
12
from wcs.workflows import Workflow
13
from wcs.formdef import FormDef
14
from wcs import fields
15

    
16
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
17

    
18

    
19
def setup_module(module):
20
    cleanup()
21

    
22
    global pub
23

    
24
    pub = create_temporary_pub()
25

    
26
    req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
27
    pub.set_app_dir(req)
28
    pub.cfg['identification'] = {'methods': ['password']}
29
    pub.write_cfg()
30

    
31

    
32
def teardown_module(module):
33
    clean_temporary_pub()
34

    
35

    
36
def create_user():
37
    if pub.user_class.get_users_with_name_identifier('user'):
38
        return pub.user_class.get_users_with_name_identifier('user')[0]
39
    user1 = pub.user_class(name='user')
40
    user1.name_identifiers = ['user']
41
    user1.is_admin = False
42
    user1.store()
43

    
44
    account1 = PasswordAccount(id='user')
45
    account1.set_password('user')
46
    account1.user_id = user1.id
47
    account1.store()
48

    
49
    pub.cfg['identification'] = {'methods': ['password']}
50
    pub.write_cfg()
51

    
52
    return user1
53

    
54

    
55
def create_formdef():
56
    FormDef.wipe()
57
    formdef = FormDef()
58
    formdef.name = 'test'
59
    formdef.fields = []
60
    formdef.store()
61
    return formdef
62

    
63

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

    
67

    
68
def test_with_user():
69
    user = create_user()
70
    app = login(get_app(pub), username='user', password='user')
71
    resp = app.get('/', status=200)
72
    resp = app.get('/myspace/', status=200)
73

    
74

    
75
def test_myspace_with_user_forms():
76
    user = create_user()
77
    formdef = create_formdef()
78

    
79
    cat = Category(name='cat')
80
    cat.store()
81
    formdef.category_id = cat.id
82
    formdef.enable_tracking_codes = True
83

    
84
    wf = Workflow(name='status')
85
    st1 = wf.add_status('Status1', 'st1')
86
    wf.store()
87
    formdef.workflow_id = wf.id
88
    formdef.store()
89

    
90
    formdef.data_class().wipe()
91
    formdata = formdef.data_class()()
92
    formdata.user_id = user.id
93
    formdata.status = 'wf-st1'
94
    formdata.receipt_time = time.localtime()
95
    formdata.data = {}
96
    formdata.store()
97
    draft = formdef.data_class()()
98
    draft.user_id = user.id
99
    draft.status = 'draft'
100
    draft.receipt_time = time.localtime()
101
    draft.data = {}
102
    draft.store()
103

    
104
    app = login(get_app(pub), username='user', password='user')
105
    resp = app.get('/myspace/')
106
    assert formdata.id != draft.id
107
    assert '<a href="/test/%s/"' % formdata.id in resp
108
    assert '<a href="/test/%s/"' % draft.id in resp
109
    resp = app.get('/test/%s/' % formdata.id, status=302)
110
    assert resp.location == 'http://example.net/cat/test/%s/' % formdata.id
111
    resp = app.get('/test/%s/' % draft.id, status=302)
112
    resp = resp.follow(status=302)
113
    assert resp.location.startswith('http://example.net/cat/test/?mt=')
114
    resp = resp.follow(status=200)
115

    
116
    # disable formdef: formdatas are still visible and accessible, drafts are not
117
    formdef.disabled = True
118
    formdef.store()
119
    resp = app.get('/myspace/')
120
    assert formdata.id != draft.id
121
    assert '<a href="/test/%s/"' % formdata.id in resp
122
    assert not '<a href="/test/%s"' % draft.id in resp
123
    resp = app.get('/cat/test/%s' % formdata.id)
124
    resp.status_int = 200
125
    resp = app.get('/cat/test/%s/' % draft.id, status=302)
126
    resp = resp.follow(status=403)
127

    
128

    
129
def test_form_category_redirection():
130
    Category.wipe()
131
    cat = Category(name='baz')
132
    cat.store()
133

    
134
    FormDef.wipe()
135
    formdef = FormDef()
136
    formdef.name = 'foobar'
137
    formdef.category_id = cat.id
138
    formdef.fields = []
139
    formdef.store()
140

    
141
    # check we get a redirection to /category/formdef/
142
    resp = get_app(pub).get('/foobar/')
143
    assert resp.location.endswith('/baz/foobar/')
144

    
145
    # check missing trailing slashs are ok
146
    resp = get_app(pub).get('/foobar')
147
    assert resp.location.endswith('/foobar/')
148
    resp = resp.follow()
149
    assert resp.location.endswith('/baz/foobar/')
150

    
151
    # check direct category access without trailing slash
152
    resp = get_app(pub).get('/baz')
153
    assert resp.location.endswith('/baz/')
(4-4/5)