Skip to content
Snippets Groups Projects

Refactor update creation api

Merged Samuel GAIST requested to merge refactor_update_creation_api into master
All threads resolved!
1 file
+ 51
4
Compare changes
  • Side-by-side
  • Inline
@@ -833,18 +833,25 @@ class LibraryUpdate(LibrariesAPIBase):
def test_no_update_without_content(self):
self.login_jackdoe()
response = self.client.put(self.url)
self.checkResponse(response, 400)
self.checkResponse(response, 200, content_type="application/json")
def test_successfull_update(self):
def test_successful_update(self):
self.login_jackdoe()
code = b"import numpy as np"
response = self.client.put(
self.url,
json.dumps({"description": "blah", "declaration": LibrariesAPIBase.UPDATE}),
json.dumps(
{
"description": "blah",
"declaration": LibrariesAPIBase.UPDATE,
"code": code,
}
),
content_type="application/json",
)
self.checkResponse(response, 204)
self.checkResponse(response, 200, content_type="application/json")
library = Library.objects.get(
author__username="jackdoe", name="personal", version=1
@@ -854,9 +861,60 @@ class LibraryUpdate(LibrariesAPIBase):
storage = beat.core.library.Storage(settings.PREFIX, library.fullname())
storage.language = "python"
self.assertTrue(storage.exists())
self.assertEqual(storage.json.load(), LibrariesAPIBase.UPDATE)
self.assertEqual(library.description, b"blah")
self.assertEqual(
json.loads(storage.json.load()), json.loads(LibrariesAPIBase.UPDATE)
)
self.assertEqual(storage.code.load(), code)
def test_successful_update_with_specific_return_field(self):
self.login_jackdoe()
code = b"""import numpy as np"""
response = self.client.put(
self.url,
json.dumps(
{
"description": "blah",
"declaration": LibrariesAPIBase.UPDATE,
"code": code,
}
),
content_type="application/json",
QUERY_STRING="fields=code",
)
def test_successfull_update_description_only(self):
self.checkResponse(response, 200, content_type="application/json")
answer = response.json()
self.assertEqual(len(answer), 1)
self.assertTrue("code" in answer)
def test_successful_update_with_specific_return_several_fields(self):
self.login_jackdoe()
code = b"""import numpy as np"""
response = self.client.put(
self.url,
json.dumps(
{
"description": "blah",
"declaration": LibrariesAPIBase.UPDATE,
"code": code,
}
),
content_type="application/json",
QUERY_STRING="fields=code,description",
)
self.checkResponse(response, 200, content_type="application/json")
answer = response.json()
self.assertEqual(len(answer), 2)
self.assertTrue("code" in answer)
self.assertTrue("description" in answer)
def test_successful_update_description_only(self):
self.login_jackdoe()
response = self.client.put(
@@ -865,14 +923,14 @@ class LibraryUpdate(LibrariesAPIBase):
content_type="application/json",
)
self.checkResponse(response, 204)
self.checkResponse(response, 200, content_type="application/json")
library = Library.objects.get(
author__username="jackdoe", name="personal", version=1
)
self.assertEqual(library.description, b"blah")
def test_successfull_update_code_only(self):
def test_successful_update_declaration_only(self):
self.login_jackdoe()
response = self.client.put(
@@ -881,7 +939,7 @@ class LibraryUpdate(LibrariesAPIBase):
content_type="application/json",
)
self.checkResponse(response, 204)
self.checkResponse(response, 200, content_type="application/json")
library = Library.objects.get(
author__username="jackdoe", name="personal", version=1
@@ -890,37 +948,59 @@ class LibraryUpdate(LibrariesAPIBase):
storage = beat.core.library.Storage(settings.PREFIX, library.fullname())
storage.language = "python"
self.assertTrue(storage.exists())
self.assertEqual(storage.json.load(), LibrariesAPIBase.UPDATE)
self.assertEqual(
json.loads(storage.json.load()), json.loads(LibrariesAPIBase.UPDATE)
)
def test_successful_update_code_only(self):
self.login_jackdoe()
code = b"import numpy as np"
response = self.client.put(
self.url, json.dumps({"code": code}), content_type="application/json"
)
self.checkResponse(response, 200, content_type="application/json")
library = Library.objects.get(
author__username="jackdoe", name="personal", version=1
)
storage = beat.core.library.Storage(settings.PREFIX, library.fullname())
storage.language = "python"
self.assertTrue(storage.exists())
self.assertEqual(storage.code.load(), code)
class LibraryRetrieval(LibrariesAPIBase):
def test_no_retrieval_of_confidential_library_for_anonymous_user(self):
url = reverse("api_libraries:object", args=["johndoe", "forked_algo"])
url = reverse("api_libraries:object", args=["johndoe", "forked_algo", 1])
response = self.client.get(url)
self.checkResponse(response, 404)
def test_fail_to_retrieve_with_invalid_username(self):
self.login_johndoe()
url = reverse("api_libraries:object", args=["unknown", "forked_algo"])
url = reverse("api_libraries:object", args=["unknown", "forked_algo", 1])
response = self.client.get(url)
self.checkResponse(response, 404)
def test_fail_to_retrieve_with_invalid_library_name(self):
self.login_johndoe()
url = reverse("api_libraries:object", args=["johndoe", "unknown"])
url = reverse("api_libraries:object", args=["johndoe", "unknown", 1])
response = self.client.get(url)
self.checkResponse(response, 404)
def test_no_retrieval_of_confidential_library(self):
self.login_jackdoe()
url = reverse("api_libraries:object", args=["johndoe", "forked_algo"])
url = reverse("api_libraries:object", args=["johndoe", "forked_algo", 1])
response = self.client.get(url)
self.checkResponse(response, 404)
def test_successful_retrieval_of_public_library_for_anonymous_user(self):
url = reverse("api_libraries:object", args=["jackdoe", "public_for_all"])
url = reverse("api_libraries:object", args=["jackdoe", "public_for_all", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -933,7 +1013,7 @@ class LibraryRetrieval(LibrariesAPIBase):
self.assertEqual(data["code"].encode("utf-8"), LibrariesAPIBase.CODE)
def test_successful_retrieval_of_usable_library_for_anonymous_user(self):
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_all"])
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_all", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -946,7 +1026,7 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_public_library(self):
self.login_johndoe()
url = reverse("api_libraries:object", args=["jackdoe", "public_for_all"])
url = reverse("api_libraries:object", args=["jackdoe", "public_for_all", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -961,7 +1041,7 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_usable_library(self):
self.login_johndoe()
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_one_user"])
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_one_user", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -974,7 +1054,7 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_publicly_usable_library(self):
self.login_johndoe()
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_all"])
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_all", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -987,7 +1067,9 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_confidential_library(self):
self.login_johndoe()
url = reverse("api_libraries:object", args=["jackdoe", "public_for_one_user"])
url = reverse(
"api_libraries:object", args=["jackdoe", "public_for_one_user", 1]
)
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -1002,7 +1084,7 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_own_public_library(self):
self.login_jackdoe()
url = reverse("api_libraries:object", args=["jackdoe", "public_for_all"])
url = reverse("api_libraries:object", args=["jackdoe", "public_for_all", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -1022,7 +1104,7 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_own_confidential_library(self):
self.login_jackdoe()
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_one_user"])
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_one_user", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -1042,7 +1124,7 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_own_usable_library(self):
self.login_jackdoe()
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_all"])
url = reverse("api_libraries:object", args=["jackdoe", "usable_by_all", 1])
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
@@ -1062,7 +1144,9 @@ class LibraryRetrieval(LibrariesAPIBase):
def test_successful_retrieval_of_own_shared_library(self):
self.login_jackdoe()
url = reverse("api_libraries:object", args=["jackdoe", "public_for_one_user"])
url = reverse(
"api_libraries:object", args=["jackdoe", "public_for_one_user", 1]
)
response = self.client.get(url)
data = self.checkResponse(response, 200, content_type="application/json")
Loading