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
96b49759
Commit
96b49759
authored
Oct 22, 2015
by
Peter Fogg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add fixture for setting config models from Bok Choy.
parent
49f9e31a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
0 deletions
+92
-0
common/test/acceptance/fixtures/config.py
+92
-0
No files found.
common/test/acceptance/fixtures/config.py
0 → 100644
View file @
96b49759
import
requests
import
re
import
json
from
lazy
import
lazy
from
.
import
LMS_BASE_URL
class
ConfigModelFixureError
(
Exception
):
"""
Error occurred while configuring the stub XQueue.
"""
pass
class
ConfigModelFixture
(
object
):
"""
Configure a ConfigurationModel by using it's JSON api.
"""
def
__init__
(
self
,
api_base
,
configuration
):
"""
Configure a ConfigurationModel exposed at `api_base` to have the configuration `configuration`.
"""
self
.
_api_base
=
api_base
self
.
_configuration
=
configuration
def
install
(
self
):
"""
Configure the stub via HTTP.
"""
url
=
LMS_BASE_URL
+
self
.
_api_base
response
=
self
.
session
.
post
(
url
,
data
=
json
.
dumps
(
self
.
_configuration
),
headers
=
self
.
headers
,
)
if
not
response
.
ok
:
raise
ConfigModelFixureError
(
"Could not configure url '{}'. response: {} - {}"
.
format
(
self
.
_api_base
,
response
,
response
.
content
,
)
)
@lazy
def
session_cookies
(
self
):
"""
Log in as a staff user, then return the cookies for the session (as a dict)
Raises a `ConfigModelFixureError` if the login fails.
"""
return
{
key
:
val
for
key
,
val
in
self
.
session
.
cookies
.
items
()}
@lazy
def
headers
(
self
):
"""
Default HTTP headers dict.
"""
return
{
'Content-type'
:
'application/json'
,
'Accept'
:
'application/json'
,
'X-CSRFToken'
:
self
.
session_cookies
.
get
(
'csrftoken'
,
''
)
}
@lazy
def
session
(
self
):
"""
Log in as a staff user, then return a `requests` `session` object for the logged in user.
Raises a `StudioApiLoginError` if the login fails.
"""
# Use auto-auth to retrieve the session for a logged in user
session
=
requests
.
Session
()
response
=
session
.
get
(
LMS_BASE_URL
+
"/auto_auth?superuser=true"
)
# Return the session from the request
if
response
.
ok
:
# auto_auth returns information about the newly created user
# capture this so it can be used by by the testcases.
user_pattern
=
re
.
compile
(
'Logged in user {0}
\
({1}
\
) with password {2} and user_id {3}'
.
format
(
'(?P<username>
\
S+)'
,
'(?P<email>[^
\
)]+)'
,
'(?P<password>
\
S+)'
,
'(?P<user_id>
\
d+)'
))
user_matches
=
re
.
match
(
user_pattern
,
response
.
text
)
if
user_matches
:
self
.
user
=
user_matches
.
groupdict
()
return
session
else
:
msg
=
"Could not log in to use ConfigModel restful API. Status code: {0}"
.
format
(
response
.
status_code
)
raise
ConfigModelFixureError
(
msg
)
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