Skip to content

Commit

Permalink
🧹 When returning an empty succes, use 204 instead of 200 (#5455)
Browse files Browse the repository at this point in the history
Fixes a part of #4029 

**How to test**
1. All unittests should pass
2. Check if all new returns are correct
  • Loading branch information
Annelein authored Apr 25, 2024
1 parent 74a94fc commit 396817d
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 33 deletions.
8 changes: 4 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static_babel_content
from markupsafe import Markup
from flask import (Flask, Response, abort, after_this_request, g,
from flask import (Flask, Response, abort, after_this_request, g, make_response,
redirect, request, send_file, url_for, jsonify,
send_from_directory, session)
from flask_babel import Babel, gettext
Expand Down Expand Up @@ -716,7 +716,7 @@ def parse_by_id(user):
program.get('level'),
program.get('lang')
)
return {}, 200
return make_response('', 204)
except BaseException:
return {"error": "parsing error"}, 200
else:
Expand Down Expand Up @@ -2265,7 +2265,7 @@ def change_language():
# Remove 'keyword_lang' from session, it will automatically be renegotiated from 'lang'
# on the next page load.
session.pop('keyword_lang')
return jsonify({'succes': 200})
return jsonify({'success': 204})


@app.route('/slides', methods=['GET'], defaults={'level': '1'})
Expand Down Expand Up @@ -2347,7 +2347,7 @@ def store_parsons_order():
}

DATABASE.store_parsons(attempt)
return jsonify({}), 200
return make_response('', 204)


@app.template_global()
Expand Down
2 changes: 1 addition & 1 deletion static/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ export function toggle_blur_code() {
export async function change_language(lang: string) {
await tryCatchPopup(async () => {
const response = await postJsonWithAchievements('/change_language', { lang });
if (response.succes) {
if (response.success) {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);

Expand Down
2 changes: 1 addition & 1 deletion static/js/appbundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -100052,7 +100052,7 @@ def note_with_error(value, err):
async function change_language(lang) {
await tryCatchPopup(async () => {
const response = await postJsonWithAchievements("/change_language", { lang });
if (response.succes) {
if (response.success) {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (lang === "en" || urlParams.get("language") !== null) {
Expand Down
4 changes: 2 additions & 2 deletions static/js/appbundle.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def get_data(self, path, expect_http_code=200, no_cookie=False, return_headers=F

def destroy_current_user(self):
assert self.username is not None
self.post_data('auth/destroy', '')
self.post_data('auth/destroy', '', 204)
# Remove any records of this user
USERS.pop(self.username)

Expand Down Expand Up @@ -585,7 +585,7 @@ def test_logout(self):

# WHEN logging out the user
# THEN receive an OK response code from the server
self.post_data('auth/logout', '')
self.post_data('auth/logout', '', 204)

# WHEN retrieving the user profile with the same cookie
# THEN first receive a redirect response code from the server, and the next
Expand Down Expand Up @@ -965,7 +965,7 @@ def test_destroy_public_profile(self):

# WHEN destroying the public profile
# THEN receive an OK response from the server
self.post_data('auth/destroy_public', public_profile, expect_http_code=200)
self.post_data('auth/destroy_public', public_profile, expect_http_code=204)


class TestProgram(AuthHelper):
Expand Down
4 changes: 2 additions & 2 deletions website/achievements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import jsonify, request, session
from flask import jsonify, make_response, request, session
from flask_babel import gettext

import hedy
Expand Down Expand Up @@ -280,4 +280,4 @@ def push_new_achievement(self, user):
)
}
)
return jsonify({})
return make_response('', 204)
6 changes: 3 additions & 3 deletions website/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import request
from flask import make_response, request
from flask_babel import gettext

import hedyweb
Expand Down Expand Up @@ -237,7 +237,7 @@ def change_user_email(self, user):
except BaseException:
return gettext("mail_error_change_processed"), 400

return {}, 200
return make_response('', 204)

@route("/getUserTags", methods=["POST"])
@requires_admin
Expand Down Expand Up @@ -273,7 +273,7 @@ def update_user_tags(self, user):
db_user["tags"] = tags

self.db.update_public_profile(username, db_user)
return {}, 200
return make_response('', 204)


def update_is_teacher(db: Database, user, is_teacher_value=1):
Expand Down
6 changes: 3 additions & 3 deletions website/auth_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def logout(self):
self.db.forget_token(request.cookies.get(TOKEN_COOKIE_NAME))
session[JUST_LOGGED_OUT] = True
remove_class_preview()
return "", 200
return make_response('', 204)

@ route("/destroy", methods=["POST"])
@ requires_login
Expand All @@ -257,14 +257,14 @@ def destroy(self, user):
self.db.forget_token(request.cookies.get(TOKEN_COOKIE_NAME))
self.db.forget_user(user["username"])
session[JUST_LOGGED_OUT] = True
return "", 200
return make_response('', 204)

@ route("/destroy_public", methods=["POST"])
@ requires_login
def destroy_public(self, user):
self.db.forget_public_profile(user["username"])
session.pop("profile_image", None) # Delete profile image id if existing
return "", 200
return make_response('', 204)

@ route("/change_student_password", methods=["POST"])
@ requires_login
Expand Down
14 changes: 7 additions & 7 deletions website/classes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid

from flask import jsonify, redirect, request, session
from flask import jsonify, make_response, redirect, request, session
from jinja_partials import render_partial
from flask_babel import gettext

Expand Down Expand Up @@ -88,7 +88,7 @@ def update_class(self, user, class_id):
achievement = self.achievements.add_single_achievement(user["username"], "on_second_thoughts")
if achievement:
return {"achievement": achievement}, 200
return {}, 200
return make_response('', 204)

@route("/<class_id>", methods=["DELETE"])
@requires_login
Expand Down Expand Up @@ -200,7 +200,7 @@ def join_class(self):
if achievement:
return {"achievement": achievement}, 200
else:
return {}, 200
return make_response('', 204)

@route("/<class_id>/student/<student_id>", methods=["DELETE"])
@requires_login
Expand All @@ -216,7 +216,7 @@ def leave_class(self, user, class_id, student_id):
achievement = self.achievements.add_single_achievement(user["username"], "detention")
if achievement:
return {"achievement": achievement}, 200
return {}, 200
return make_response('', 204)

@route("/<class_id>/second-teacher/<second_teacher>", methods=["DELETE"])
@requires_login
Expand Down Expand Up @@ -357,7 +357,7 @@ def invite_student(self, user):
"invited_as_text": gettext("student"),
}
self.db.add_class_invite(data)
return {}, 200
return make_response('', 204)

@route("/invite-second-teacher", methods=["POST"])
@requires_teacher
Expand Down Expand Up @@ -404,7 +404,7 @@ def invite_second_teaceher(self, user):
"invited_as_text": gettext("second_teacher"),
}
self.db.add_class_invite(data)
return {}, 200
return make_response('', 204)

@route("/remove_student_invite", methods=["POST"])
@requires_login
Expand All @@ -429,7 +429,7 @@ def remove_invite(self, user):
return utils.error_page(error=404, ui_message=gettext("no_such_class"))

self.db.remove_user_class_invite(username, class_id)
return {}, 200
return make_response('', 204)

@route("/hedy/l/<link_id>", methods=["GET"])
def resolve_class_link(self, link_id):
Expand Down
6 changes: 3 additions & 3 deletions website/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from difflib import SequenceMatcher
import re
from flask import g, jsonify, request
from flask import g, jsonify, make_response, request
from flask_babel import gettext
import utils
import hedy_content
Expand Down Expand Up @@ -820,7 +820,7 @@ def remove_common_error_item(self, user, class_id, error_id):
self.db.update_class_errors(common_errors)
break

return {}, 200
return make_response('', 204)

def retrieve_exceptions_per_student(self, class_id):
"""
Expand Down Expand Up @@ -954,7 +954,7 @@ def select_levels(self, user, class_id):

self.db.update_class_customizations(class_customization)

return {}, 200
return make_response('', 204)


def add(username, action):
Expand Down
5 changes: 3 additions & 2 deletions website/tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import request, g
from flask import make_response, request, g
from flask_babel import gettext
import jinja_partials
import uuid
Expand Down Expand Up @@ -89,7 +89,8 @@ def delete_from_adventure(self, user, tag, adventure_id):
# This only deletes a tag from an adventure.
# TODO: perhaps allow admin to permanently delete a tag.
if not tag or not adventure_id:
return {}, 200
# is this not suppossed to be an error response?
return make_response('', 204)

tag_name = tag.strip()
db_tag = self.db.read_tag(tag_name)
Expand Down
4 changes: 2 additions & 2 deletions website/user_activity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import os
from flask import request, session
from flask import make_response, request, session

# import utils
from config import config
Expand Down Expand Up @@ -62,6 +62,6 @@ def index(self, user):

try:
logger.log(data)
return {}, 200
return make_response('', 204)
except IOError:
return "Not logged", 400

0 comments on commit 396817d

Please sign in to comment.