Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-notes-api
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-notes-api
Commits
da69127e
Commit
da69127e
authored
Dec 12, 2014
by
Oleg Marshev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use factory.
parent
a82ae9fd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
35 deletions
+33
-35
notesapi/v1/tests/helpers.py
+17
-3
notesapi/v1/tests/test_views.py
+16
-32
No files found.
notesapi/v1/tests/helpers.py
View file @
da69127e
...
@@ -16,6 +16,21 @@ def get_id_token(user):
...
@@ -16,6 +16,21 @@ def get_id_token(user):
'exp'
:
timegm
((
now
+
timedelta
(
seconds
=
300
))
.
utctimetuple
()),
'exp'
:
timegm
((
now
+
timedelta
(
seconds
=
300
))
.
utctimetuple
()),
},
settings
.
CLIENT_SECRET
)
},
settings
.
CLIENT_SECRET
)
NOTE
=
{
"user"
:
'test-user-id'
,
"usage_id"
:
"test-usage-id"
,
"course_id"
:
"test-course-id"
,
"text"
:
"test note text"
,
"quote"
:
"test note quote"
,
"ranges"
:
[
{
"start"
:
"/p[1]"
,
"end"
:
"/p[1]"
,
"startOffset"
:
0
,
"endOffset"
:
10
,
}
],
}
class
AnnotationFactory
(
object
):
class
AnnotationFactory
(
object
):
"""
"""
...
@@ -27,8 +42,8 @@ class AnnotationFactory(object):
...
@@ -27,8 +42,8 @@ class AnnotationFactory(object):
Create multiple notes directly in elasticsearch.
Create multiple notes directly in elasticsearch.
"""
"""
for
i
in
xrange
(
1
,
number
+
1
):
for
i
in
xrange
(
1
,
number
+
1
):
kwargs
=
{
'id'
:
str
(
i
),
'text'
:
'Text {}'
.
format
(
i
)}
NOTE
.
update
({
'id'
:
str
(
i
),
'text'
:
'Text {}'
.
format
(
i
)})
_create_annotation
(
refresh
=
False
,
**
kwargs
)
_create_annotation
(
refresh
=
False
,
**
NOTE
)
es
.
conn
.
indices
.
refresh
(
es
.
index
)
es
.
conn
.
indices
.
refresh
(
es
.
index
)
...
@@ -37,7 +52,6 @@ def _create_annotation(refresh=True, **kwargs):
...
@@ -37,7 +52,6 @@ def _create_annotation(refresh=True, **kwargs):
"""
"""
Create annotation directly in elasticsearch.
Create annotation directly in elasticsearch.
"""
"""
kwargs
[
'user'
]
=
'test-user-id'
return
Annotation
(
**
kwargs
)
.
save
(
refresh
=
refresh
)
return
Annotation
(
**
kwargs
)
.
save
(
refresh
=
refresh
)
...
...
notesapi/v1/tests/test_views.py
View file @
da69127e
...
@@ -12,7 +12,7 @@ from rest_framework.test import APITestCase
...
@@ -12,7 +12,7 @@ from rest_framework.test import APITestCase
from
annotator
import
es
,
auth
from
annotator
import
es
,
auth
from
annotator.annotation
import
Annotation
from
annotator.annotation
import
Annotation
from
.helpers
import
get_id_token
,
AnnotationFactory
,
_create_annotation
,
_get_annotation
from
.helpers
import
get_id_token
,
AnnotationFactory
,
_create_annotation
,
_get_annotation
,
NOTE
TEST_USER
=
"test-user-id"
TEST_USER
=
"test-user-id"
...
@@ -31,21 +31,7 @@ class BaseAnnotationViewTests(APITestCase):
...
@@ -31,21 +31,7 @@ class BaseAnnotationViewTests(APITestCase):
self
.
client
.
credentials
(
HTTP_X_ANNOTATOR_AUTH_TOKEN
=
token
)
self
.
client
.
credentials
(
HTTP_X_ANNOTATOR_AUTH_TOKEN
=
token
)
self
.
headers
=
{
"user"
:
TEST_USER
}
self
.
headers
=
{
"user"
:
TEST_USER
}
self
.
payload
=
{
self
.
payload
=
NOTE
.
copy
()
"user"
:
TEST_USER
,
"usage_id"
:
"test-usage-id"
,
"course_id"
:
"test-course-id"
,
"text"
:
"test note text"
,
"quote"
:
"test note quote"
,
"ranges"
:
[
{
"start"
:
"/p[1]"
,
"end"
:
"/p[1]"
,
"startOffset"
:
0
,
"endOffset"
:
10
,
}
],
}
self
.
expected_note
=
{
self
.
expected_note
=
{
"created"
:
"2014-11-26T00:00:00+00:00"
,
"created"
:
"2014-11-26T00:00:00+00:00"
,
...
@@ -158,15 +144,13 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -158,15 +144,13 @@ class AnnotationViewTests(BaseAnnotationViewTests):
Ensure we can get an existing annotation.
Ensure we can get an existing annotation.
"""
"""
mock_datetime
.
datetime
.
now
.
return_value
.
isoformat
.
return_value
=
"2014-11-26T00:00:00+00:00"
mock_datetime
.
datetime
.
now
.
return_value
.
isoformat
.
return_value
=
"2014-11-26T00:00:00+00:00"
note
=
self
.
payload
AnnotationFactory
.
create
()
note
[
'id'
]
=
"test_id"
_create_annotation
(
**
note
)
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
"test_id"
})
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
1
})
response
=
self
.
client
.
get
(
url
,
self
.
headers
)
response
=
self
.
client
.
get
(
url
,
self
.
headers
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
expected_note
[
'id'
]
=
'test_id'
self
.
expected_note
.
update
({
'id'
:
'1'
,
'text'
:
"Text 1"
})
self
.
assertEqual
(
response
.
data
,
self
.
expected_note
)
self
.
assertEqual
(
response
.
data
,
self
.
expected_note
)
def
test_read_notfound
(
self
):
def
test_read_notfound
(
self
):
...
@@ -181,14 +165,14 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -181,14 +165,14 @@ class AnnotationViewTests(BaseAnnotationViewTests):
"""
"""
Ensure we can update an existing annotation.
Ensure we can update an existing annotation.
"""
"""
_create_annotation
(
text
=
u"Foo"
,
id
=
'123'
,
created
=
'2014-10-10'
)
AnnotationFactory
.
create
(
)
payload
=
{
'id'
:
'123'
,
'text'
:
'Bar'
}
payload
=
{
'id'
:
1
,
'text'
:
'Bar'
}
payload
.
update
(
self
.
headers
)
payload
.
update
(
self
.
headers
)
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
1
23
})
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
1
})
response
=
self
.
client
.
put
(
url
,
payload
,
format
=
'json'
)
response
=
self
.
client
.
put
(
url
,
payload
,
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
annotation
=
_get_annotation
(
'123'
)
annotation
=
_get_annotation
(
1
)
self
.
assertEqual
(
annotation
[
'text'
],
"Bar"
,
"annotation wasn't updated in db"
)
self
.
assertEqual
(
annotation
[
'text'
],
"Bar"
,
"annotation wasn't updated in db"
)
self
.
assertEqual
(
response
.
data
[
'text'
],
"Bar"
,
"update annotation should be returned in response"
)
self
.
assertEqual
(
response
.
data
[
'text'
],
"Bar"
,
"update annotation should be returned in response"
)
...
@@ -198,15 +182,15 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -198,15 +182,15 @@ class AnnotationViewTests(BaseAnnotationViewTests):
Tests if id is used from URL, regardless of what arrives in JSON payload.
Tests if id is used from URL, regardless of what arrives in JSON payload.
"""
"""
_create_annotation
(
text
=
u"Foo"
,
id
=
'123'
)
AnnotationFactory
.
create
(
)
payload
=
{
'text'
:
'Bar'
}
payload
=
{
'text'
:
'Bar'
}
payload
.
update
(
self
.
headers
)
payload
.
update
(
self
.
headers
)
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
1
23
})
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
1
})
response
=
self
.
client
.
put
(
url
,
payload
,
format
=
'json'
)
response
=
self
.
client
.
put
(
url
,
payload
,
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
annotation
=
_get_annotation
(
'123'
)
annotation
=
_get_annotation
(
1
)
self
.
assertEqual
(
annotation
[
'text'
],
"Bar"
,
"annotation wasn't updated in db"
)
self
.
assertEqual
(
annotation
[
'text'
],
"Bar"
,
"annotation wasn't updated in db"
)
def
test_update_with_wrong_payload_id
(
self
):
def
test_update_with_wrong_payload_id
(
self
):
...
@@ -215,15 +199,15 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -215,15 +199,15 @@ class AnnotationViewTests(BaseAnnotationViewTests):
Tests if id is used from URL, regardless of what arrives in JSON payload.
Tests if id is used from URL, regardless of what arrives in JSON payload.
"""
"""
_create_annotation
(
text
=
u"Foo"
,
id
=
'123'
)
AnnotationFactory
.
create
(
)
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
1
23
})
url
=
reverse
(
'api:v1:annotations_detail'
,
kwargs
=
{
'annotation_id'
:
1
})
payload
=
{
'text'
:
'Bar'
,
'id'
:
'abc'
}
payload
=
{
'text'
:
'Bar'
,
'id'
:
'abc'
}
payload
.
update
(
self
.
headers
)
payload
.
update
(
self
.
headers
)
response
=
self
.
client
.
put
(
url
,
payload
,
format
=
'json'
)
response
=
self
.
client
.
put
(
url
,
payload
,
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
annotation
=
_get_annotation
(
'123'
)
annotation
=
_get_annotation
(
1
)
self
.
assertEqual
(
annotation
[
'text'
],
"Bar"
,
"annotation wasn't updated in db"
)
self
.
assertEqual
(
annotation
[
'text'
],
"Bar"
,
"annotation wasn't updated in db"
)
def
test_update_notfound
(
self
):
def
test_update_notfound
(
self
):
...
@@ -245,7 +229,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
...
@@ -245,7 +229,7 @@ class AnnotationViewTests(BaseAnnotationViewTests):
response
=
self
.
client
.
delete
(
url
,
self
.
headers
)
response
=
self
.
client
.
delete
(
url
,
self
.
headers
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
,
"response should be 204 NO CONTENT"
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
,
"response should be 204 NO CONTENT"
)
self
.
assertEqual
(
_get_annotation
(
'1'
),
None
,
"annotation wasn't deleted in db"
)
self
.
assertEqual
(
_get_annotation
(
1
),
None
,
"annotation wasn't deleted in db"
)
def
test_delete_notfound
(
self
):
def
test_delete_notfound
(
self
):
"""
"""
...
...
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