mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-26 02:54:20 +00:00
Fix markdown api and add test for user picture page
This commit is contained in:
parent
293369f165
commit
b9d19be183
@ -1,5 +1,6 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
from ninja import Form
|
||||||
from ninja_extra import ControllerBase, api_controller, route
|
from ninja_extra import ControllerBase, api_controller, route
|
||||||
from ninja_extra.exceptions import PermissionDenied
|
from ninja_extra.exceptions import PermissionDenied
|
||||||
|
|
||||||
|
@ -14,26 +14,21 @@
|
|||||||
document.head.innerHTML += '<link rel="stylesheet" href="' + css + '">';
|
document.head.innerHTML += '<link rel="stylesheet" href="' + css + '">';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom markdown parser
|
|
||||||
function customMarkdownParser(plainText, cb) {
|
|
||||||
$.ajax({
|
|
||||||
url: "{{ markdown_api_url }}",
|
|
||||||
method: "POST",
|
|
||||||
data: { text: plainText, csrfmiddlewaretoken: getCSRFToken() },
|
|
||||||
}).done(cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pretty markdown input
|
// Pretty markdown input
|
||||||
const easymde = new EasyMDE({
|
const easymde = new EasyMDE({
|
||||||
element: document.getElementById("{{ widget.attrs.id }}"),
|
element: document.getElementById("{{ widget.attrs.id }}"),
|
||||||
spellChecker: false,
|
spellChecker: false,
|
||||||
autoDownloadFontAwesome: false,
|
autoDownloadFontAwesome: false,
|
||||||
previewRender: function(plainText, preview) { // Async method
|
previewRender: function (plainText, preview) {
|
||||||
clearTimeout(lastAPICall);
|
clearTimeout(lastAPICall);
|
||||||
lastAPICall = setTimeout(() => {
|
lastAPICall = setTimeout(async () => {
|
||||||
customMarkdownParser(plainText, (msg) => preview.innerHTML = msg);
|
const res = await fetch("{{ markdown_api_url }}", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({ text: plainText }),
|
||||||
|
});
|
||||||
|
preview.innerHTML = await res.text();
|
||||||
}, 300);
|
}, 300);
|
||||||
return preview.innerHTML;
|
return null;
|
||||||
},
|
},
|
||||||
forceSync: true, // Avoid validation error on generic create view
|
forceSync: true, // Avoid validation error on generic create view
|
||||||
toolbar: [
|
toolbar: [
|
||||||
|
@ -217,7 +217,7 @@ def test_full_markdown_syntax():
|
|||||||
assert result == html
|
assert result == html
|
||||||
|
|
||||||
|
|
||||||
class PageHandlingTest(TestCase):
|
class TestPageHandling(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
cls.root = User.objects.get(username="root")
|
cls.root = User.objects.get(username="root")
|
||||||
@ -320,11 +320,16 @@ http://git.an
|
|||||||
assertInHTML(expected, response.content.decode())
|
assertInHTML(expected, response.content.decode())
|
||||||
|
|
||||||
|
|
||||||
class UserToolsTest:
|
@pytest.mark.django_db
|
||||||
|
class TestUserTools:
|
||||||
def test_anonymous_user_unauthorized(self, client):
|
def test_anonymous_user_unauthorized(self, client):
|
||||||
"""An anonymous user shouldn't have access to the tools page."""
|
"""An anonymous user shouldn't have access to the tools page."""
|
||||||
response = client.get(reverse("core:user_tools"))
|
response = client.get(reverse("core:user_tools"))
|
||||||
assert response.status_code == 403
|
assertRedirects(
|
||||||
|
response,
|
||||||
|
expected_url=f"/login?next=%2Fuser%2Ftools%2F",
|
||||||
|
target_status_code=301,
|
||||||
|
)
|
||||||
|
|
||||||
@pytest.mark.parametrize("username", ["guy", "root", "skia", "comunity"])
|
@pytest.mark.parametrize("username", ["guy", "root", "skia", "comunity"])
|
||||||
def test_page_is_working(self, client, username):
|
def test_page_is_working(self, client, username):
|
||||||
@ -335,13 +340,47 @@ class UserToolsTest:
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
class TestUserPicture:
|
||||||
|
def test_anonymous_user_unauthorized(self, client):
|
||||||
|
"""An anonymous user shouldn't have access to an user's photo page."""
|
||||||
|
response = client.get(
|
||||||
|
reverse(
|
||||||
|
"core:user_pictures",
|
||||||
|
kwargs={"user_id": User.objects.get(username="sli").pk},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert response.status_code == 403
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("username", "status"),
|
||||||
|
[
|
||||||
|
("guy", 403),
|
||||||
|
("root", 200),
|
||||||
|
("skia", 200),
|
||||||
|
("sli", 200),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_page_is_working(self, client, username, status):
|
||||||
|
"""Only user that subscribed (or admins) should be able to see the page."""
|
||||||
|
# Test for simple user
|
||||||
|
client.force_login(User.objects.get(username=username))
|
||||||
|
response = client.get(
|
||||||
|
reverse(
|
||||||
|
"core:user_pictures",
|
||||||
|
kwargs={"user_id": User.objects.get(username="sli").pk},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert response.status_code == status
|
||||||
|
|
||||||
|
|
||||||
# TODO: many tests on the pages:
|
# TODO: many tests on the pages:
|
||||||
# - renaming a page
|
# - renaming a page
|
||||||
# - changing a page's parent --> check that page's children's full_name
|
# - changing a page's parent --> check that page's children's full_name
|
||||||
# - changing the different groups of the page
|
# - changing the different groups of the page
|
||||||
|
|
||||||
|
|
||||||
class FileHandlingTest(TestCase):
|
class TestFileHandling(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
cls.subscriber = User.objects.get(username="subscriber")
|
cls.subscriber = User.objects.get(username="subscriber")
|
||||||
@ -377,7 +416,7 @@ class FileHandlingTest(TestCase):
|
|||||||
assert "ls</a>" in str(response.content)
|
assert "ls</a>" in str(response.content)
|
||||||
|
|
||||||
|
|
||||||
class UserIsInGroupTest(TestCase):
|
class TestUserIsInGroup(TestCase):
|
||||||
"""Test that the User.is_in_group() and AnonymousUser.is_in_group()
|
"""Test that the User.is_in_group() and AnonymousUser.is_in_group()
|
||||||
work as intended.
|
work as intended.
|
||||||
"""
|
"""
|
||||||
@ -518,7 +557,7 @@ class UserIsInGroupTest(TestCase):
|
|||||||
assert self.skia.is_in_group(name="This doesn't exist") is False
|
assert self.skia.is_in_group(name="This doesn't exist") is False
|
||||||
|
|
||||||
|
|
||||||
class DateUtilsTest(TestCase):
|
class TestDateUtils(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
cls.autumn_month = settings.SITH_SEMESTER_START_AUTUMN[0]
|
cls.autumn_month = settings.SITH_SEMESTER_START_AUTUMN[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user