Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
9e856851
Commit
9e856851
authored
May 08, 2014
by
asadiqbal08
Committed by
Jonathan Piacenti
Aug 20, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fields_need_visible_updatable_via_api_761: merged with master
parent
913252d4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
227 additions
and
29 deletions
+227
-29
lms/djangoapps/api_manager/users/tests.py
+146
-26
lms/djangoapps/api_manager/users/views.py
+81
-3
No files found.
lms/djangoapps/api_manager/users/tests.py
View file @
9e856851
...
...
@@ -8,7 +8,8 @@ from random import randint
import
simplejson
as
json
import
unittest
import
uuid
from
mock
import
patch
from
django.utils.translation
import
ugettext
as
_
from
django.conf
import
settings
from
django.contrib.auth.models
import
User
from
django.core.cache
import
cache
...
...
@@ -22,7 +23,9 @@ TEST_API_KEY = str(uuid.uuid4())
class
SecureClient
(
Client
):
""" Django test client using a "secure" connection. """
def
__init__
(
self
,
*
args
,
**
kwargs
):
kwargs
=
kwargs
.
copy
()
kwargs
.
update
({
'SERVER_PORT'
:
443
,
'wsgi.url_scheme'
:
'https'
})
...
...
@@ -31,7 +34,9 @@ class SecureClient(Client):
@override_settings
(
MODULESTORE
=
TEST_DATA_MIXED_MODULESTORE
)
@override_settings
(
EDX_API_KEY
=
TEST_API_KEY
)
@patch.dict
(
"django.conf.settings.FEATURES"
,
{
'ENFORCE_PASSWORD_POLICY'
:
False
})
class
UsersApiTests
(
TestCase
):
""" Test suite for Users API views """
def
setUp
(
self
):
...
...
@@ -41,6 +46,7 @@ class UsersApiTests(TestCase):
self
.
test_email
=
str
(
uuid
.
uuid4
())
+
'@test.org'
self
.
test_first_name
=
str
(
uuid
.
uuid4
())
self
.
test_last_name
=
str
(
uuid
.
uuid4
())
self
.
test_city
=
str
(
uuid
.
uuid4
())
self
.
client
=
SecureClient
()
cache
.
clear
()
...
...
@@ -52,7 +58,8 @@ class UsersApiTests(TestCase):
}
json_data
=
json
.
dumps
(
data
)
response
=
self
.
client
.
post
(
uri
,
headers
=
headers
,
content_type
=
'application/json'
,
data
=
json_data
)
response
=
self
.
client
.
post
(
uri
,
headers
=
headers
,
content_type
=
'application/json'
,
data
=
json_data
)
return
response
def
do_get
(
self
,
uri
):
...
...
@@ -76,11 +83,13 @@ class UsersApiTests(TestCase):
def
test_user_list_post
(
self
):
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
self
.
assertGreater
(
response
.
data
[
'id'
],
0
)
confirm_uri
=
self
.
test_server_prefix
+
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
confirm_uri
=
self
.
test_server_prefix
+
\
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
self
.
assertEqual
(
response
.
data
[
'uri'
],
confirm_uri
)
self
.
assertEqual
(
response
.
data
[
'email'
],
self
.
test_email
)
self
.
assertEqual
(
response
.
data
[
'username'
],
local_username
)
...
...
@@ -90,7 +99,9 @@ class UsersApiTests(TestCase):
def
test_user_list_post_inactive
(
self
):
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
,
'is_active'
:
False
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
,
'is_active'
:
False
}
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
self
.
assertEqual
(
response
.
data
[
'is_active'
],
False
)
...
...
@@ -98,7 +109,8 @@ class UsersApiTests(TestCase):
def
test_user_list_post_duplicate
(
self
):
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
409
)
...
...
@@ -108,7 +120,8 @@ class UsersApiTests(TestCase):
def
test_user_detail_get
(
self
):
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
response
=
self
.
do_get
(
test_uri
)
...
...
@@ -131,7 +144,8 @@ class UsersApiTests(TestCase):
def
test_user_detail_post
(
self
):
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
data
=
{
'is_active'
:
False
}
...
...
@@ -142,6 +156,81 @@ class UsersApiTests(TestCase):
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
data
[
'is_active'
],
False
)
def
test_user_detail_post_username
(
self
):
"""
Create two users, then pass the same first username in request in order to update username of second user.
Must return bad request against username, Already exist!
"""
lst_username
=
[]
test_uri
=
'/api/users'
for
i
in
xrange
(
2
):
local_username
=
self
.
test_username
+
str
(
i
)
lst_username
.
append
(
local_username
)
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
,
'city'
:
self
.
test_city
,
'country'
:
'PK'
,
'level_of_education'
:
'b'
,
'year_of_birth'
:
'2000'
,
"gender"
:
'male'
}
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
data
[
"username"
]
=
lst_username
[
0
]
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
409
)
# Pass an invalid username in order to update username.
# Must return bad request against. invalid username!
data
[
"username"
]
=
'@'
response
=
self
.
do_post
(
test_uri
,
data
)
message
=
_
(
'Username should only consist of A-Z and 0-9, with no spaces.'
)
self
.
assertEqual
(
response
.
status_code
,
400
)
self
.
assertEqual
(
response
.
data
[
'message'
],
message
)
def
test_user_detail_post_user_profile_added_updated
(
self
):
"""
Create a user, then add the user profile
Must be added
"""
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
,
'city'
:
self
.
test_city
,
'country'
:
'PK'
,
'level_of_education'
:
'b'
,
'year_of_birth'
:
'2000'
,
"gender"
:
'male'
}
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
response
=
self
.
do_get
(
test_uri
)
self
.
is_user_profile_created_updated
(
response
,
data
)
# Testing profile updating scenario.
# Must be updated
data
[
"country"
]
=
"US"
data
[
"year_of_birth"
]
=
"1990"
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
200
)
response
=
self
.
do_get
(
test_uri
)
self
.
is_user_profile_created_updated
(
response
,
data
)
def
test_user_detail_post_profile_added_invalid_year
(
self
):
"""
Create a user, then add the user profile with invalid year of birth
Profile Must be added with year_of_birth will be none
"""
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
,
'city'
:
self
.
test_city
,
'country'
:
'PK'
,
'level_of_education'
:
'b'
,
'year_of_birth'
:
'abcd'
,
"gender"
:
'male'
}
response
=
self
.
do_post
(
test_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
test_uri_1
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
response
=
self
.
do_get
(
test_uri_1
)
data
[
"year_of_birth"
]
=
'None'
self
.
is_user_profile_created_updated
(
response
,
data
)
def
test_user_detail_post_invalid_user
(
self
):
test_uri
=
'/api/users/123124124'
data
=
{
'is_active'
:
False
}
...
...
@@ -155,7 +244,8 @@ class UsersApiTests(TestCase):
group_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
...
...
@@ -177,7 +267,8 @@ class UsersApiTests(TestCase):
group_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
response
=
self
.
do_get
(
test_uri
)
...
...
@@ -206,7 +297,8 @@ class UsersApiTests(TestCase):
group_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
...
...
@@ -233,7 +325,8 @@ class UsersApiTests(TestCase):
group_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
+
'/groups'
...
...
@@ -255,7 +348,8 @@ class UsersApiTests(TestCase):
group_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
test_uri
=
test_uri
+
'/'
+
str
(
response
.
data
[
'id'
])
+
'/groups'
data
=
{
'group_id'
:
group_id
}
...
...
@@ -265,7 +359,8 @@ class UsersApiTests(TestCase):
self
.
assertEqual
(
response
.
status_code
,
204
)
response
=
self
.
do_get
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
404
)
response
=
self
.
do_delete
(
test_uri
)
# Relationship no longer exists, should get a 204 all the same
response
=
self
.
do_delete
(
test_uri
)
# Relationship no longer exists, should get a 204 all the same
self
.
assertEqual
(
response
.
status_code
,
204
)
response
=
self
.
do_get
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
404
)
...
...
@@ -282,7 +377,8 @@ class UsersApiTests(TestCase):
group_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users/'
+
str
(
user_id
)
+
'/groups/'
+
str
(
group_id
)
...
...
@@ -293,7 +389,8 @@ class UsersApiTests(TestCase):
course
=
CourseFactory
.
create
()
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
'{}/{}/courses'
.
format
(
test_uri
,
str
(
user_id
))
...
...
@@ -317,7 +414,8 @@ class UsersApiTests(TestCase):
def
test_user_courses_list_post_undefined_course
(
self
):
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
'{}/{}/courses'
.
format
(
test_uri
,
str
(
user_id
))
...
...
@@ -329,7 +427,8 @@ class UsersApiTests(TestCase):
course
=
CourseFactory
.
create
(
display_name
=
"TEST COURSE"
)
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
'{}/{}/courses'
.
format
(
test_uri
,
str
(
user_id
))
...
...
@@ -372,7 +471,8 @@ class UsersApiTests(TestCase):
)
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
test_uri
+
'/'
+
str
(
user_id
)
+
'/courses'
...
...
@@ -423,7 +523,8 @@ class UsersApiTests(TestCase):
)
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
test_uri
+
'/'
+
str
(
user_id
)
+
'/courses'
...
...
@@ -452,7 +553,8 @@ class UsersApiTests(TestCase):
)
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
test_uri
+
'/'
+
str
(
user_id
)
+
'/courses'
...
...
@@ -485,7 +587,8 @@ class UsersApiTests(TestCase):
course
=
CourseFactory
.
create
()
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
'/api/users/'
+
str
(
user_id
)
+
'/courses/'
+
str
(
course
.
id
)
...
...
@@ -496,7 +599,8 @@ class UsersApiTests(TestCase):
course
=
CourseFactory
.
create
()
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
post_uri
=
test_uri
+
'/'
+
str
(
user_id
)
+
'/courses'
...
...
@@ -510,7 +614,8 @@ class UsersApiTests(TestCase):
self
.
assertEqual
(
response
.
status_code
,
204
)
response
=
self
.
do_get
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
404
)
response
=
self
.
do_post
(
post_uri
,
data
)
# Re-enroll the student in the course
response
=
self
.
do_post
(
post_uri
,
data
)
# Re-enroll the student in the course
self
.
assertEqual
(
response
.
status_code
,
201
)
response
=
self
.
do_get
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
200
)
...
...
@@ -522,16 +627,31 @@ class UsersApiTests(TestCase):
def
test_user_courses_detail_delete_undefined_user
(
self
):
course
=
CourseFactory
.
create
()
user_id
=
'2134234'
test_uri
=
'/api/users/{}/courses/{}'
.
format
(
str
(
user_id
),
str
(
course
.
id
))
test_uri
=
'/api/users/{}/courses/{}'
.
format
(
str
(
user_id
),
str
(
course
.
id
))
response
=
self
.
do_delete
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
204
)
def
test_user_courses_detail_delete_undefined_course
(
self
):
test_uri
=
'/api/users'
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
self
.
test_first_name
,
'last_name'
:
self
.
test_last_name
}
response
=
self
.
do_post
(
test_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
test_uri
=
'{}/{}/oasdf987sdf'
.
format
(
test_uri
,
str
(
user_id
))
response
=
self
.
do_delete
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
404
)
def
is_user_profile_created_updated
(
self
,
response
,
data
):
"""This function compare response with user profile data """
fullname
=
'{} {}'
.
format
(
self
.
test_first_name
,
self
.
test_last_name
)
self
.
assertEqual
(
response
.
data
[
'full_name'
],
fullname
)
self
.
assertEqual
(
response
.
data
[
'city'
],
data
[
"city"
])
self
.
assertEqual
(
response
.
data
[
'country'
],
data
[
"country"
])
self
.
assertEqual
(
response
.
data
[
'gender'
],
data
[
"gender"
])
self
.
assertEqual
(
response
.
data
[
'level_of_education'
],
data
[
"level_of_education"
])
self
.
assertEqual
(
str
(
response
.
data
[
'year_of_birth'
]),
data
[
"year_of_birth"
])
lms/djangoapps/api_manager/users/views.py
View file @
9e856851
...
...
@@ -11,6 +11,7 @@ from django.utils.translation import get_language, ugettext_lazy as _
from
rest_framework
import
status
from
rest_framework.response
import
Response
from
rest_framework.views
import
APIView
from
django.db.models
import
Q
from
api_manager.models
import
GroupProfile
from
api_manager.permissions
import
ApiKeyHeaderPermission
...
...
@@ -30,7 +31,6 @@ from util.bad_request_rate_limiter import BadRequestRateLimiter
log
=
logging
.
getLogger
(
__name__
)
AUDIT_LOG
=
logging
.
getLogger
(
"audit"
)
def
_generate_base_uri
(
request
):
"""
Constructs the protocol:host:path component of the resource uri
...
...
@@ -46,6 +46,17 @@ def _generate_base_uri(request):
return
resource_uri
def
_serialize_user_profile
(
response_data
,
user_profile
):
"""This function serialize user profile """
response_data
[
'full_name'
]
=
user_profile
.
name
response_data
[
'city'
]
=
user_profile
.
city
response_data
[
'country'
]
=
user_profile
.
country
.
code
response_data
[
'level_of_education'
]
=
user_profile
.
level_of_education
response_data
[
'year_of_birth'
]
=
user_profile
.
year_of_birth
response_data
[
'gender'
]
=
user_profile
.
gender
return
response_data
def
_serialize_user
(
response_data
,
user
):
"""
Loads the object data into the response dict
...
...
@@ -59,7 +70,6 @@ def _serialize_user(response_data, user):
response_data
[
'is_active'
]
=
user
.
is_active
return
response_data
def
_save_module_position
(
request
,
user
,
course_id
,
course_descriptor
,
position
):
"""
Records the indicated position for the specified course
...
...
@@ -109,6 +119,11 @@ class UsersList(APIView):
first_name
=
request
.
DATA
.
get
(
'first_name'
,
''
)
last_name
=
request
.
DATA
.
get
(
'last_name'
,
''
)
is_active
=
request
.
DATA
.
get
(
'is_active'
,
None
)
city
=
request
.
DATA
.
get
(
'city'
,
''
)
country
=
request
.
DATA
.
get
(
'country'
,
''
)
level_of_education
=
request
.
DATA
.
get
(
'level_of_education'
,
''
)
year_of_birth
=
request
.
DATA
.
get
(
'year_of_birth'
,
''
)
gender
=
request
.
DATA
.
get
(
'gender'
,
''
)
# enforce password complexity as an optional feature
if
settings
.
FEATURES
.
get
(
'ENFORCE_PASSWORD_POLICY'
,
False
):
...
...
@@ -149,6 +164,18 @@ class UsersList(APIView):
profile
=
UserProfile
(
user
=
user
)
profile
.
name
=
'{} {}'
.
format
(
first_name
,
last_name
)
profile
.
city
=
city
profile
.
country
=
country
profile
.
level_of_education
=
level_of_education
profile
.
gender
=
gender
try
:
profile
.
year_of_birth
=
int
(
year_of_birth
)
except
ValueError
:
# If they give us garbage, just ignore it instead
# of asking them to put an integer.
profile
.
year_of_birth
=
None
profile
.
save
()
UserPreference
.
set_preference
(
user
,
LANGUAGE_KEY
,
get_language
())
...
...
@@ -170,7 +197,7 @@ class UsersList(APIView):
response_data
[
'uri'
]
=
'{}/{}'
.
format
(
base_uri
,
str
(
user
.
id
))
else
:
status_code
=
status
.
HTTP_409_CONFLICT
response_data
[
'message'
]
=
"User '
%
s' already exists"
,
username
response_data
[
'message'
]
=
"User '
%
s' already exists"
%
(
username
)
response_data
[
'field_conflict'
]
=
"username"
return
Response
(
response_data
,
status
=
status_code
)
...
...
@@ -194,6 +221,11 @@ class UsersDetail(APIView):
response_data
[
'resources'
]
.
append
({
'uri'
:
resource_uri
})
resource_uri
=
'{}/courses'
.
format
(
base_uri
)
response_data
[
'resources'
]
.
append
({
'uri'
:
resource_uri
})
existing_user_profile
=
UserProfile
.
objects
.
get
(
user_id
=
user_id
)
if
existing_user_profile
:
_serialize_user_profile
(
response_data
,
existing_user_profile
)
return
Response
(
response_data
,
status
=
status
.
HTTP_200_OK
)
except
ObjectDoesNotExist
:
return
Response
(
response_data
,
status
=
status
.
HTTP_404_NOT_FOUND
)
...
...
@@ -203,6 +235,15 @@ class UsersDetail(APIView):
POST provides the ability to update information about an existing user
"""
response_data
=
{}
first_name
=
request
.
DATA
.
get
(
'first_name'
,
''
)
last_name
=
request
.
DATA
.
get
(
'last_name'
,
''
)
city
=
request
.
DATA
.
get
(
'city'
,
''
)
country
=
request
.
DATA
.
get
(
'country'
,
''
)
level_of_education
=
request
.
DATA
.
get
(
'level_of_education'
,
''
)
year_of_birth
=
request
.
DATA
.
get
(
'year_of_birth'
,
''
)
gender
=
request
.
DATA
.
get
(
'gender'
,
''
)
base_uri
=
_generate_base_uri
(
request
)
response_data
[
'uri'
]
=
_generate_base_uri
(
request
)
# Add some rate limiting here by re-using the RateLimitMixin as a helper class
...
...
@@ -217,6 +258,26 @@ class UsersDetail(APIView):
limiter
.
tick_bad_request_counter
(
request
)
existing_user
=
None
if
existing_user
:
username
=
request
.
DATA
.
get
(
'username'
,
None
)
if
username
:
try
:
validate_slug
(
username
)
except
ValidationError
:
status_code
=
status
.
HTTP_400_BAD_REQUEST
response_data
[
'message'
]
=
_
(
'Username should only consist of A-Z and 0-9, with no spaces.'
)
return
Response
(
response_data
,
status
=
status_code
)
existing_username
=
User
.
objects
.
filter
(
username
=
username
)
.
filter
(
~
Q
(
id
=
user_id
))
if
existing_username
:
status_code
=
status
.
HTTP_409_CONFLICT
response_data
[
'message'
]
=
"User '
%
s' already exists"
%
(
username
)
response_data
[
'field_conflict'
]
=
"username"
return
Response
(
response_data
,
status
=
status_code
)
existing_user
.
username
=
username
response_data
[
'username'
]
=
existing_user
.
username
existing_user
.
save
()
is_active
=
request
.
DATA
.
get
(
'is_active'
,
None
)
if
is_active
is
not
None
:
...
...
@@ -278,6 +339,23 @@ class UsersDetail(APIView):
password_history_entry
=
PasswordHistory
()
password_history_entry
.
create
(
existing_user
)
existing_user_profile
=
UserProfile
.
objects
.
get
(
user_id
=
user_id
)
if
existing_user_profile
:
existing_user_profile
.
name
=
'{} {}'
.
format
(
first_name
,
last_name
)
existing_user_profile
.
city
=
city
existing_user_profile
.
country
=
country
existing_user_profile
.
level_of_education
=
level_of_education
existing_user_profile
.
gender
=
gender
try
:
existing_user_profile
.
year_of_birth
=
int
(
year_of_birth
)
except
ValueError
:
# If they give us garbage, just ignore it instead
# of asking them to put an integer.
existing_user_profile
.
year_of_birth
=
None
existing_user_profile
.
save
()
status_code
=
status
.
HTTP_200_OK
else
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment