All files / components/ApiKeys NewApiKeyModal.jsx

100% Statements 6/6
100% Branches 5/5
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                              33x 26x     7x 1x 1x     7x                                                                                                                      
import React from "react";
import { Alert, Button } from "react-bootstrap";
import Modal from "react-bootstrap/Modal";
import { toast } from "react-toastify";
 
import { formatTime } from "main/utils/dateUtils";
import {
  API_KEYS_DOCS_URL,
  studentInfoCurlExample,
} from "main/utils/apiKeyUtils";
 
/**
 * Shows a key that was just created. This is the only time the key is ever
 * visible, so the modal says so and offers a copy button.
 *
 * @param issuedKey the response of POST /api/courses/key, or null to hide
 */
export default function NewApiKeyModal({
  issuedKey,
  courseId,
  onClose,
  testIdPrefix = "NewApiKeyModal",
}) {
  if (!issuedKey) {
    return null;
  }
 
  const copyKey = async () => {
    await navigator.clipboard.writeText(issuedKey.key);
    toast("API key copied to clipboard.");
  };
 
  return (
    <Modal
      show={true}
      onHide={onClose}
      centered={true}
      size="lg"
      data-testid={`${testIdPrefix}-modal`}
    >
      <Modal.Header closeButton>
        <Modal.Title>Your new API key</Modal.Title>
      </Modal.Header>
      <Modal.Body>
        <Alert variant="warning" data-testid={`${testIdPrefix}-warning`}>
          Copy this key now. Frontiers does not store it, so it cannot be shown
          again. If you lose it, revoke it and create a new one.
        </Alert>
        <p>
          Key: <code data-testid={`${testIdPrefix}-key`}>{issuedKey.key}</code>
          <Button
            size="sm"
            variant="outline-secondary"
            className="ms-2"
            onClick={copyKey}
            data-testid={`${testIdPrefix}-copy`}
          >
            Copy
          </Button>
        </p>
        <p data-testid={`${testIdPrefix}-details`}>
          {issuedKey.label ? `Label: ${issuedKey.label}. ` : ""}
          Expires: {formatTime(issuedKey.expiresAt)}.
        </p>
        <p data-testid={`${testIdPrefix}-example-intro`}>
          Example: look up a student the way a Gradescope autograder would (see{" "}
          <a
            href={API_KEYS_DOCS_URL}
            target="_blank"
            rel="noopener noreferrer"
            data-testid={`${testIdPrefix}-docs-link`}
          >
            the API key documentation
          </a>{" "}
          for the other endpoints):
        </p>
        <pre
          className="bg-light p-2 rounded"
          data-testid={`${testIdPrefix}-example`}
        >
          {studentInfoCurlExample(courseId, issuedKey.key)}
        </pre>
      </Modal.Body>
      <Modal.Footer>
        <Button onClick={onClose} data-testid={`${testIdPrefix}-close`}>
          I have copied the key
        </Button>
      </Modal.Footer>
    </Modal>
  );
}