Page Menu
Home
Search
Configure Global Search
Log In
Files
F13101225
test_users.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
3 KB
Subscribers
None
test_users.py
View Options
import
bson
from
pillar.tests
import
AbstractPillarTest
class
UsersTest
(
AbstractPillarTest
):
def
setUp
(
self
,
**
kwargs
):
super
()
.
setUp
(
**
kwargs
)
self
.
group_names
=
[
f
'test{num}'
for
num
in
range
(
10
)]
self
.
group_map
=
self
.
create_standard_groups
(
additional_groups
=
self
.
group_names
)
def
test_add_user_to_group_happy
(
self
):
from
pillar.api
import
users
user_id
=
bson
.
ObjectId
(
24
*
'1'
)
self
.
create_user
(
user_id
,
roles
=
{
'subscriber'
},
groups
=
[
self
.
group_map
[
'subscriber'
]])
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertEqual
([
self
.
group_map
[
'subscriber'
]],
db_user
[
'groups'
])
with
self
.
app
.
test_request_context
():
users
.
add_user_to_group
(
user_id
,
self
.
group_map
[
'test1'
])
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertEqual
([
self
.
group_map
[
'subscriber'
],
self
.
group_map
[
'test1'
],
],
db_user
[
'groups'
])
def
test_add_user_to_group_no_initial_groups
(
self
):
from
pillar.api
import
users
user_id
=
bson
.
ObjectId
(
24
*
'1'
)
self
.
create_user
(
user_id
,
roles
=
set
())
with
self
.
app
.
test_request_context
():
# Ensure the user doesn't even have a 'groups' property.
users_coll
=
self
.
app
.
db
(
'users'
)
users_coll
.
update_one
({
'_id'
:
user_id
},
{
'$unset'
:
{
'groups'
:
1
}})
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertNotIn
(
'groups'
,
db_user
)
users
.
add_user_to_group
(
user_id
,
self
.
group_map
[
'test1'
])
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertEqual
([
self
.
group_map
[
'test1'
],
],
db_user
[
'groups'
])
def
test_remove_user_from_group_happy
(
self
):
from
pillar.api
import
users
user_id
=
bson
.
ObjectId
(
24
*
'1'
)
self
.
create_user
(
user_id
,
roles
=
{
'subscriber'
},
groups
=
[
self
.
group_map
[
'subscriber'
],
self
.
group_map
[
'test1'
],
])
# Remove from existing group
with
self
.
app
.
test_request_context
():
users
.
remove_user_from_group
(
user_id
,
self
.
group_map
[
'test1'
])
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertEqual
([
self
.
group_map
[
'subscriber'
]],
db_user
[
'groups'
])
# Remove same group again, should be no-op
with
self
.
app
.
test_request_context
():
users
.
remove_user_from_group
(
user_id
,
self
.
group_map
[
'test1'
])
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertEqual
([
self
.
group_map
[
'subscriber'
]],
db_user
[
'groups'
])
# Remove from last group, should result in empty list.
with
self
.
app
.
test_request_context
():
users
.
remove_user_from_group
(
user_id
,
self
.
group_map
[
'subscriber'
])
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertEqual
([],
db_user
[
'groups'
])
# Remove non-existing group from empty list, should also work.
with
self
.
app
.
test_request_context
():
users
.
remove_user_from_group
(
user_id
,
bson
.
ObjectId
())
db_user
=
self
.
fetch_user_from_db
(
user_id
)
self
.
assertEqual
([],
db_user
[
'groups'
])
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Wed, May 25, 8:41 PM (2 d)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
4d/fa/48a0d7f8d4c6e1b33943a2107d2d
Attached To
rPS Pillar
Event Timeline
Log In to Comment