mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-24 16:57:30 +00:00
Fix user creation with group_ids via API (#12965)
* Fix user creation with group_ids via API Creating a user via POST /api/user/ with the 'group_ids' field raised TypeError: User() got unexpected keyword arguments: 'group_ids'. The write-only 'group_ids' field was only handled on update. - Declare 'group_ids' in SKIP_CREATE_FIELDS so it is not passed to the model - Assign the groups after the user instance is created - Add API test covering user creation with and without groups * Bump API version
This commit is contained in:
1 parent
6c4d2ef347
commit
575fbdc907
3 files changed
+50
-1
No files matched your search
@@ -154,6 +154,43 @@ class UserAPITests(InvenTreeAPITestCase):
|
||||
self.assertEqual(response.data['username'], 'Superuser')
|
||||
self.assertEqual(response.data['is_superuser'], True)
|
||||
|
||||
def test_user_create_with_groups(self):
|
||||
"""Test that groups can be assigned when creating a new user via the API."""
|
||||
url = reverse('api-user-list')
|
||||
|
||||
group_a = Group.objects.create(name='Group A')
|
||||
group_b = Group.objects.create(name='Group B')
|
||||
|
||||
self.user.is_staff = True
|
||||
self.user.save()
|
||||
self.assignRole('admin.add')
|
||||
|
||||
data = {
|
||||
'username': 'grouped',
|
||||
'first_name': 'Grouped',
|
||||
'last_name': 'User',
|
||||
'email': 'grouped@example.org',
|
||||
'group_ids': [group_a.pk, group_b.pk],
|
||||
}
|
||||
|
||||
response = self.post(url, data=data, expected_code=201)
|
||||
|
||||
self.assertEqual(response.data['username'], 'grouped')
|
||||
|
||||
group_names = {g['name'] for g in response.data['groups']}
|
||||
self.assertEqual(group_names, {'Group A', 'Group B'})
|
||||
|
||||
user = User.objects.get(username='grouped')
|
||||
self.assertEqual(set(user.groups.all()), {group_a, group_b})
|
||||
|
||||
# Creating a user without specifying groups must still work
|
||||
response = self.post(
|
||||
url,
|
||||
data={**data, 'username': 'ungrouped', 'group_ids': []},
|
||||
expected_code=201,
|
||||
)
|
||||
self.assertEqual(response.data['groups'], [])
|
||||
|
||||
def test_user_detail(self):
|
||||
"""Test the UserDetail API endpoint."""
|
||||
user = User.objects.first()
|
||||
|
||||
Reference in new issue
Block a user