def test_cannot_save_duplicate_challenge(self): c = Challenge.objects.create(name='Hello', difficulty=5, score=10, test_case_count=5, category=self.sub_cat, description=self.sample_desc) c.save() with self.assertRaises(ValidationError): c = Challenge(name='Hello', difficulty=5, score=10, test_case_count=5, category=self.sub_cat, description=self.sample_desc) c.full_clean()
def test_cannot_save_blank_challenge(self): c = Challenge() with self.assertRaises(Exception): c.full_clean()
def bulk_update(request: HttpRequest): try: challenges_data = json.loads(request.body.decode()) challenges = challenges_data["challenges"] except Exception: logger.exception("Couldn't load json") return JsonResponse({"message": "Couldn't load json data"}, status=400) try: assert isinstance(challenges, list) assert len(challenges) > 0 and isinstance(challenges[0], dict) except AssertionError: logger.exception( "Expected a list of dict, representing the challenges") return JsonResponse( { "message": "Expected a list of dict, representing the challenges" }, status=400) try: challenge_slugs = {chal["slug"] for chal in challenges} assert len(challenge_slugs) == len(challenges) except (KeyError, AssertionError): return JsonResponse( { "message": "`slug` should be present and unique in all challenges" }, status=400) expected_data = """Challenges should be of this form (following documentation may not be up to date): { "difficulty": 0-4 (0 easier, 4 harder), "name": "bla bla" (len<50), "slug": "bla-bla" (extracted from name, identifies the challenge), "description": "Fun story in html", "category": Any of: "MIC", "WEB", "PPC", "FOR", "REV", "PWN", "CRY", "NET", "flag": "INSA{the flag}" (len<=255), "static_url": null OR "url of the static files for the chal", "company_logo_url": null OR "the URL of the company who wrote the chal, if any", "nb_points_override": integer, if greater then -3, it will override the automagic points calculus }""" for chal in challenges: try: difficulty = chal["difficulty"] name = chal["name"] slug = chal["slug"] description = chal["description"] category = chal["category"] flag = sha256(chal["flag"].encode('utf-8')).hexdigest() static_url = chal["static_url"] company_logo_url = chal["company_logo_url"] nb_points_override = chal["nb_points_override"] except KeyError: logger.exception("Wrong challenge format") return JsonResponse({"message": expected_data}, status=400) try: try: chal = Challenge.all_objects.get(slug=slug) chal.difficulty = difficulty chal.name = name chal.description = description chal.category = category chal.flag = flag chal.static_url = static_url chal.company_logo_url = company_logo_url chal.nb_points_override = nb_points_override chal.full_clean() chal.save() except ObjectDoesNotExist: chal = Challenge(difficulty=difficulty, name=name, slug=slug, description=description, category=category, flag=flag, static_url=static_url, company_logo_url=company_logo_url, nb_points_override=nb_points_override) chal.full_clean() chal.save() except ValidationError as e: logger.exception("Wrong challenge format") return JsonResponse( { "message": "Challenge `{}` doesn't have the right form: {}.\n{}". format(name, e, expected_data) }, status=400) except Exception as e: logger.exception("Exception creating the challenge") return JsonResponse( { "message": "Error while updating {}, please check the serverlogs. err: {}" "\nFor reference: {}".format(name, e, expected_data) }, status=500) Challenge.all_objects.exclude(slug__in=challenge_slugs).delete() return JsonResponse({"message": "OK"}, status=200)