2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2010-2012 Cidadania S. Coop. Galega
6 # This file is part of e-cidadania.
8 # e-cidadania is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # e-cidadania is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with e-cidadania. If not, see <http://www.gnu.org/licenses/>.
23 from django
.contrib
.auth
.models
import User
24 from django
.core
.urlresolvers
import reverse
26 from django
.test
import Client
27 from django
.test
import TestCase
29 from django
.utils
.encoding
import smart_str
31 from core
.spaces
.models
import Space
33 from apps
.ecidadania
.debate
.models
import Debate
34 from apps
.ecidadania
.debate
.models
import Column
35 from apps
.ecidadania
.debate
.models
import Row
36 from apps
.ecidadania
.debate
.models
import Note
39 from tests
.data_seeder
import seeder
42 class ECDTestCase(TestCase
):
43 """Class which extends Django TestCase and adds methods specific to
44 e-cidadania for testing.
48 """Performs set up for the tests.
50 self
.client
= Client(enforce_csrf_checks
=False)
51 self
.username
= 'dummy_username'
52 self
.user_password
= 'dummy_user_password'
53 self
.admin_username
= 'admin_username'
54 self
.admin_password
= 'admin_password'
55 self
.user
= self
.create_user(self
.username
, self
.user_password
)
56 self
.admin
= self
.create_super_user(self
.admin_username
,
59 space_properties
= {'name': 'user_space', 'url': 'user_space_url',
60 'author': self
.user
, 'public': True,
61 'mod_debate': True, 'mod_proposals': True,
62 'mod_news': True, 'mod_cal': True, 'mod_docs': True
64 self
.user_space
= self
.seed(Space
, properties
=space_properties
)
65 space_properties
.update({'author': self
.admin
, 'name': 'admin_space',
66 'url': 'admin_space_url'})
67 self
.admin_space
= self
.seed(Space
, properties
=space_properties
)
69 debate_properties
= {'space': self
.user_space
, 'author': self
.user
}
70 self
.user_debate
= self
.seed(Debate
, properties
=debate_properties
)
72 debate_properties
.update({'space': self
.admin_space
,
73 'author': self
.admin
})
74 self
.admin_debate
= self
.seed(Debate
, properties
=debate_properties
)
76 column_properties
= {'debate': self
.user_debate
, 'criteria': 'user'}
77 self
.user_column
= Column(**column_properties
).save()
78 column_properties
.update({'debate': self
.admin_debate
,
80 self
.admin_column
= Column(**column_properties
)
82 row_properties
= column_properties
.copy()
83 self
.admin_row
= self
.seed(Row
, properties
=row_properties
)
85 row_properties
.update({'debate': self
.user_debate
})
86 self
.user_row
= self
.seed(Row
, properties
=row_properties
)
88 note_properties
= {'column': self
.user_column
, 'row': self
.user_row
,
89 'debate': self
.user_debate
, 'author': self
.user
}
90 self
.user_note
= self
.seed(Note
, properties
=note_properties
)
92 note_properties
.update({'column': self
.admin_column
,
93 'row': self
.admin_row
,
94 'debate': self
.admin_debate
,
95 'author': self
.admin
})
96 self
.admin_note
= self
.seed(Note
, properties
=note_properties
)
98 def seed(self
, model
, properties
=None, constraints
=None, follow_fk
=None,
99 generate_fk
=None, follow_m2m
=None, factory
=None, commit
=True):
100 """Generates and returns a new instance of the `model` with
101 properties in `properties`.
103 instance
= seeder
.seed(model
=model
, constraints
=constraints
,
104 follow_fk
=follow_fk
, generate_fk
=None,
105 follow_m2m
=None, factory
=None,
106 model_properties
=properties
, commit
=commit
)
110 def seedn(self
, count
, model
, properties
, constraints
=None, follow_fk
=None,
111 generate_fk
=None, follow_m2m
=None, factory
=None, commit
=True):
112 """Generates and returns `count` number of instances of `model` with
113 properties in `properties`.
116 obj_list
= seeder
.seedn(count
=count
, model
=model
, constraints
=constraints
,
117 follow_fk
=follow_fk
, generate_fk
=generate_fk
,
118 follow_m2m
=follow_m2m
, factory
=factory
,
119 model_properties
=properties
, commit
=True)
122 def create_user(self
, username
, password
, email
=None, properties
=None,
124 """Creates, saves and returns a user with a given username, password
125 and email. If `properties` is supplied, it will be applied to the
129 user
= User
.objects
.create_user(username
=username
, password
=password
,
132 for key
in properties
:
133 setattr(user
, key
, properties
[key
])
137 # log out the current user
139 # log in the new user
140 user
= self
.login(username
=username
, password
=password
, email
=email
)
143 def create_super_user(self
, username
='admin', password
='admin_pass',
144 email
='admin@test.com', properties
=None,
146 """Creates, saves and returns a super user with a given username,
147 password and email. If `properties` is supplied, it will be applied
151 super_user
= User
.objects
.create_superuser(username
=username
,
155 for key
in properties
:
156 setattr(super_user
, key
, properties
[key
])
161 super_user
= self
.login(username
=username
, password
=password
,
165 def login(self
, username
, password
, email
=None):
166 """Logs in a user with the given username and password. If the user is
167 not present in the database, it will be created and logged in.
169 We assume `username` is unique across the database.
173 user
= User
.objects
.get(username
=username
)
177 user
= self
.create_user(username
=username
, password
=password
,
180 self
.client
.login(username
=username
, password
=password
)
185 """Logs out the currently logged in user.
190 def isLoggedIn(self
, user
=None):
191 """Checks and returns True if a user is logged in otherwise returns
195 if '_auth_user_id' not in self
.client
.session
:
198 if (user
.pk
== self
.client
.session
['_auth_user_id']):
203 def getURL(self
, name
, args
=None, kwargs
=None):
204 """Returns the url for the given `name` which may be a function name or
207 return reverse(name
, args
=args
, kwargs
=kwargs
)
209 def get(self
, url
=None, url_name
=None, data
={}, follow
=False, **extra
):
211 Performs a get to the given url and returns the response.
213 if url
is None and url_name
is None:
214 raise Exception("Please pass either url or url name")
217 url
= self
.getURL(url_name
)
219 response
= self
.client
.get(url
, data
=data
, follow
=follow
, extra
=extra
)
222 def post(self
, url
, data
={}, follow
=False, **extra
):
224 Performs a post to the supplied url and returns the response.
227 response
= self
.client
.post(path
=url
, data
=data
, follow
=follow
,
231 def printResponse(self
, response
):
232 """Prints the response to the terminal.
233 We need this method because the response is a unicode string and
234 results in exception when printed directly i.e print response.
237 return smart_str(response
)
240 def assertResponseCode(self
, response
, status_code
):
241 """Asserts that the response status is status_code.
243 if response
.status_code
!= status_code
:
248 httplib
.FORBIDDEN
, httplib
.BAD_REQUEST
, httplib
.NOT_FOUND
,
250 url_codes
= [httplib
.NOT_FOUND
]
252 if response
.status_code
in verbose_codes
:
255 if response
.context
and response
.status_code
in message_codes
:
257 print response
.context
['message']
261 if response
.status_code
in url_codes
:
262 print response
.request
['PATH_INFO']
264 self
.assertEqual(status_code
, response
.status_code
)
266 def assertResponseOK(self
, response
):
267 """Asserts that the response status is OK.
269 self
.assertResponseCode(response
, httplib
.OK
)
271 def assertResponseRedirect(self
, response
):
272 """Asserts that the response status is FOUND.
274 self
.assertResponseCode(response
, httplib
.FOUND
)
276 def assertResponseForbidden(self
, response
):
277 """Asserts that the response status is FORBIDDEN.
279 self
.assertResponseCode(response
, httplib
.FORBIDDEN
)
281 def assertResponseBadRequest(self
, response
):
282 """Asserts that the response status is BAD_REQUEST.
284 self
.assertResponseCode(response
, httplib
.BAD_REQUEST
)
286 def assertResponseNotFound(self
, response
):
287 """Asserts that the response status is NOT_FOUND.
289 self
.assertResponseCode(response
, httplib
.NOT_FOUND
)