Commit 57b98b86 authored by Samuel GAIST's avatar Samuel GAIST

[experiments][api] Implement experiment reset end point

parent 589a77ce
......@@ -42,7 +42,7 @@ from rest_framework import generics
from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework.reverse import reverse
from rest_framework.errors import ParseError
from rest_framework.exceptions import ParseError
import beat.core.hash
import beat.core.algorithm
......@@ -681,6 +681,40 @@ class CancelExperimentView(APIView):
# ----------------------------------------------------------
class ResetExperimentView(APIView):
"""
Reset an experiment
"""
permission_classes = [permissions.IsAuthenticated]
def post(
self, request, author_name, toolchain_author_name, toolchain_name, version, name
):
if toolchain_author_name is None:
toolchain_author_name = author_name
# Retrieve the experiment
experiment = get_object_or_404(
Experiment,
author__username=author_name,
toolchain__author__username=toolchain_author_name,
toolchain__name=toolchain_name,
toolchain__version=version,
name=name,
)
self.check_object_permissions(request, experiment)
experiment.reset()
return Response(status=200)
# ----------------------------------------------------------
class RetrieveExperimentResultsFromAttestationView(
CommonContextMixin, generics.RetrieveAPIView
):
......
......@@ -56,7 +56,7 @@ from .models import CachedFile
from .models import Block
from .models import Result
TEST_PWD = "1234"
TEST_PWD = "1234" # nosec
HASHES = {
"addition1": "ff59a471cec5c17b45d1dfa5aff3ed897ee2d7ed87de205365b372be1c726c87",
......@@ -1095,6 +1095,68 @@ class ExperimentStartingAPI(ExperimentTestBase):
# ----------------------------------------------------------
class ExperimentResetingAPI(ExperimentTestBase):
def setUp(self):
super(ExperimentResetingAPI, self).setUp()
self.login_johndoe()
url = reverse("api_experiments:list_create", args=["johndoe"])
response = self.client.post(
url,
json.dumps(
{
"toolchain": "johndoe/toolchain1/1",
"declaration": ExperimentTestBase.DECLARATION1,
"name": "pending",
}
),
content_type="application/json",
)
self.checkResponse(response, 201, content_type="application/json")
self.client.logout()
self.url = reverse(
"api_experiments:reset",
args=["johndoe", "johndoe", "toolchain1", 1, "pending"],
)
def test_no_access_for_anonymous_user(self):
response = self.client.post(self.url)
self.checkResponse(response, 403)
def test_no_access_for_other_user(self):
self.login_jackdoe()
response = self.client.post(self.url)
self.checkResponse(response, 403)
def test_reset_experiment(self):
self.login_johndoe()
experiments = Experiment.objects.all()
self.assertEqual(experiments.count(), 1)
experiment = experiments[0]
experiment.status = Experiment.DONE
experiment.start_date = datetime.now()
experiment.end_date = datetime.now()
experiment.save()
response = self.client.post(self.url)
self.checkResponse(response, 200)
experiment.refresh_from_db()
self.assertTrue(experiment.creation_date is not None)
self.assertTrue(experiment.start_date is None)
self.assertTrue(experiment.end_date is None)
self.assertEqual(experiment.status, Experiment.PENDING)
# ----------------------------------------------------------
class ResultsAPI(ExperimentTestBase):
def setUp(self):
super(ResultsAPI, self).setUp()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment