Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action_flush name to flush survey data button #77

Merged
merged 3 commits into from
May 9, 2024

Conversation

petechd
Copy link
Contributor

@petechd petechd commented May 7, 2024

What is the context of this PR?

This reintroduces the form name "action_flush" so our logic (referring it) works as expected. It was removed in formatting and linting PR.

How to review

To test flush survey data working you need to:

  • open a schema using launcher
  • answer few questions
  • before submitting (the whole survey) go back to the launcher screen
  • load a previous value for response id field and pick the same schema name
  • Important: choose the flusher role
  • press the flush survey data button
    You should hit the flush endpoint and get status code 200.
    In other scenarios you should end up on the /flush endpoint and get different statuses. Just follow the logic below.
    The endpoint logic in runner:
@flush_blueprint.route("/flush", methods=["POST"])
def flush_data() -> Response:
    if session:
        session.clear()

    encrypted_token = request.args.get("token")

    if not encrypted_token or encrypted_token is None:
        return Response(status=403)

    decrypted_token = decrypt(
        token=encrypted_token,
        key_store=_get_keystore(),
        key_purpose=KEY_PURPOSE_AUTHENTICATION,
        leeway=current_app.config["EQ_JWT_LEEWAY_IN_SECONDS"],
    )

    roles = decrypted_token.get("roles")

    if roles and "flusher" in roles:
        user = _get_user(decrypted_token["response_id"])

        if metadata := get_metadata(user):
            contextvars.bind_contextvars(
                tx_id=metadata.tx_id,
                ce_id=metadata.collection_exercise_sid,
            )
            bind_contextvars_schema_from_metadata(metadata)

        if _submit_data(user):
            return Response(status=200)
        return Response(status=404)
    return Response(status=403)

the logic in launcher:

flushAction := r.PostForm.Get("action_flush")
	log.Println("Request: " + r.PostForm.Encode())

	if flushAction != "" {
		http.Redirect(w, r, hostURL+"/flush?token="+token, 307)
	} else if launchVersion != "" {
		http.Redirect(w, r, hostURL+"/session?token="+token, 301)
	} else {
		http.Error(w, fmt.Sprintf("Invalid Action"), 500)
	}

To make results more obvious you can modify the runner code to something like:

        if _submit_data(user):
            return "<h1>Successfully flushed</h1>"
        return Response(status=404)
    return Response(status=403)

Copy link

@Yuyuutsu Yuyuutsu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as expected 👍

@petechd petechd merged commit bfb7cf6 into main May 9, 2024
3 checks passed
@petechd petechd deleted the add-back-flush-survey-action branch May 9, 2024 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants