30 |
30 |
from django.core import mail
|
31 |
31 |
from django.urls import reverse
|
32 |
32 |
from django.utils.encoding import force_text
|
|
33 |
from django.utils.text import slugify
|
33 |
34 |
from django.utils.timezone import now
|
34 |
35 |
from django.utils.http import urlencode
|
35 |
36 |
|
... | ... | |
1254 |
1255 |
assert role.ou == get_default_ou()
|
1255 |
1256 |
|
1256 |
1257 |
|
|
1258 |
def test_api_post_ou_no_slug(app, superuser):
|
|
1259 |
app.authorization = ('Basic', (superuser.username, superuser.username))
|
|
1260 |
OU = get_ou_model()
|
|
1261 |
|
|
1262 |
ou_data = {
|
|
1263 |
'name': 'Some Organizational Unit',
|
|
1264 |
}
|
|
1265 |
resp = app.post_json('/api/ous/', params=ou_data)
|
|
1266 |
uuid = resp.json['uuid']
|
|
1267 |
ou = OU.objects.get(uuid=uuid)
|
|
1268 |
assert ou.id != get_default_ou().id
|
|
1269 |
assert ou.slug == slugify(ou.name)
|
|
1270 |
assert ou.slug == slugify(ou_data['name'])
|
|
1271 |
# another call with same ou name
|
|
1272 |
ou_data = {
|
|
1273 |
'name': 'Some Organizational Unit',
|
|
1274 |
}
|
|
1275 |
resp = app.post_json('/api/ous/', params=ou_data, status=400)
|
|
1276 |
assert resp.json['errors']['__all__'] == [
|
|
1277 |
"The fields name must make a unique set.",
|
|
1278 |
"The fields slug must make a unique set."
|
|
1279 |
]
|
|
1280 |
# no slug no name
|
|
1281 |
ou_data = {
|
|
1282 |
'id': 42,
|
|
1283 |
}
|
|
1284 |
resp = app.post_json('/api/ous/', params=ou_data, status=400)
|
|
1285 |
assert resp.json['errors']['name'] == ['This field is required.']
|
|
1286 |
|
|
1287 |
|
|
1288 |
def test_api_post_ou_get_or_create(app, superuser):
|
|
1289 |
app.authorization = ('Basic', (superuser.username, superuser.username))
|
|
1290 |
OU = get_ou_model()
|
|
1291 |
# first get-or-create? -> create
|
|
1292 |
ou_data = {
|
|
1293 |
'name': 'Some Organizational Unit',
|
|
1294 |
}
|
|
1295 |
resp = app.post_json('/api/ous/', params=ou_data)
|
|
1296 |
uuid = resp.json['uuid']
|
|
1297 |
ou = OU.objects.get(uuid=uuid)
|
|
1298 |
# second get-or-create? -> get
|
|
1299 |
ou_data = {
|
|
1300 |
'name': 'Some Organizational Unit',
|
|
1301 |
'slug': ou.slug,
|
|
1302 |
}
|
|
1303 |
resp = app.post_json('/api/ous/?get_or_create=slug', params=ou_data)
|
|
1304 |
assert resp.json['uuid'] == ou.uuid
|
|
1305 |
# update-or-create? -> update
|
|
1306 |
ou_data = {
|
|
1307 |
'name': 'Another name',
|
|
1308 |
'slug': ou.slug,
|
|
1309 |
}
|
|
1310 |
resp = app.post_json('/api/ous/?update_or_create=slug', params=ou_data)
|
|
1311 |
assert resp.json['uuid'] == ou.uuid
|
|
1312 |
assert OU.objects.get(uuid=resp.json['uuid']).name == ou_data['name']
|
|
1313 |
|
|
1314 |
|
1257 |
1315 |
def test_api_post_role_unauthorized(app, simple_user, ou1):
|
1258 |
1316 |
app.authorization = ('Basic', (simple_user.username, simple_user.username))
|
1259 |
1317 |
Role = get_role_model()
|
1260 |
|
-
|