CourseApiKeyController.java

1
package edu.ucsb.cs156.frontiers.controllers;
2
3
import edu.ucsb.cs156.frontiers.entities.Course;
4
import edu.ucsb.cs156.frontiers.entities.CourseApiKey;
5
import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.frontiers.models.CourseApiKeyView;
7
import edu.ucsb.cs156.frontiers.repositories.CourseRepository;
8
import edu.ucsb.cs156.frontiers.services.ApiKeyService;
9
import edu.ucsb.cs156.frontiers.services.ApiKeyService.ExpirationChoice;
10
import edu.ucsb.cs156.frontiers.services.ApiKeyService.IssuedCourseApiKey;
11
import io.swagger.v3.oas.annotations.Operation;
12
import io.swagger.v3.oas.annotations.Parameter;
13
import io.swagger.v3.oas.annotations.tags.Tag;
14
import jakarta.validation.constraints.Size;
15
import java.util.List;
16
import lombok.extern.slf4j.Slf4j;
17
import org.springframework.http.ResponseEntity;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.validation.annotation.Validated;
20
import org.springframework.web.bind.annotation.DeleteMapping;
21
import org.springframework.web.bind.annotation.GetMapping;
22
import org.springframework.web.bind.annotation.PostMapping;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestParam;
25
import org.springframework.web.bind.annotation.RestController;
26
27
@Tag(name = "Course API Key")
28
@RestController
29
@Slf4j
30
@RequestMapping("/api/courses/key")
31
@Validated
32
public class CourseApiKeyController extends ApiController {
33
34
  private final ApiKeyService apiKeyService;
35
  private final CourseRepository courseRepository;
36
37
  public CourseApiKeyController(ApiKeyService apiKeyService, CourseRepository courseRepository) {
38
    super();
39
    this.apiKeyService = apiKeyService;
40
    this.courseRepository = courseRepository;
41
  }
42
43
  @Operation(
44
      summary = "Create an API key for a course (instructor only)",
45
      description =
46
          "The key is returned once, in this response, and cannot be retrieved again. "
47
              + "Requires the ENABLE_API_KEYS course option.")
48
  @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)")
49
  @PostMapping("")
50
  public IssuedCourseApiKey createApiKey(
51
      @Parameter(name = "courseId") @RequestParam Long courseId,
52
      @Parameter(name = "choice") @RequestParam ExpirationChoice choice,
53
      @Parameter(name = "label", description = "Optional short name for the key (at most 60 chars)")
54
          @RequestParam(required = false)
55
          @Size(max = 60, message = "label must be at most 60 characters")
56
          String label) {
57
    Course course =
58
        courseRepository
59
            .findById(courseId)
60 1 1. lambda$createApiKey$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::lambda$createApiKey$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Course.class, courseId));
61 1 1. createApiKey : negated conditional → KILLED
    if (!apiKeyService.apiKeysEnabled(courseId)) {
62
      throw new IllegalArgumentException(
63
          "The course option ENABLE_API_KEYS must be enabled to create an API key.");
64
    }
65 1 1. createApiKey : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::createApiKey → KILLED
    return apiKeyService.createApiKey(course, choice, label);
66
  }
67
68
  @Operation(
69
      summary = "List the API keys of a course (instructor only)",
70
      description =
71
          "Every key ever created for the course, newest first, with its status "
72
              + "(ACTIVE, EXPIRED or REVOKED) and usage. The keys themselves are never returned.")
73
  @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)")
74
  @GetMapping("")
75
  public List<CourseApiKeyView> listApiKeys(
76
      @Parameter(name = "courseId") @RequestParam Long courseId) {
77
    courseRepository
78
        .findById(courseId)
79 1 1. lambda$listApiKeys$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::lambda$listApiKeys$0 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(Course.class, courseId));
80 1 1. listApiKeys : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::listApiKeys → KILLED
    return apiKeyService.listApiKeys(courseId);
81
  }
82
83
  @Operation(summary = "Revoke one API key of a course, by id (instructor only)")
84
  @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)")
85
  @DeleteMapping("")
86
  public Object revokeApiKeyById(
87
      @Parameter(name = "courseId") @RequestParam Long courseId,
88
      @Parameter(name = "id") @RequestParam Long id) {
89 1 1. revokeApiKeyById : negated conditional → KILLED
    if (!apiKeyService.revokeApiKeyById(id, courseId)) {
90
      throw new EntityNotFoundException(CourseApiKey.class, id);
91
    }
92 1 1. revokeApiKeyById : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::revokeApiKeyById → KILLED
    return genericMessage("API key with id %d revoked".formatted(id));
93
  }
94
95
  @Operation(
96
      summary = "Revoke an API key, given the key itself (no login needed)",
97
      description = "For scripts that hold a key and need to disable it, e.g. after a leak.")
98
  @DeleteMapping("/revoke")
99
  public ResponseEntity<Void> revokeApiKey(
100
      @Parameter(name = "apiKey") @RequestParam String apiKey) {
101 2 1. revokeApiKey : negated conditional → KILLED
2. revokeApiKey : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::revokeApiKey → KILLED
    return apiKeyService.revokeApiKey(apiKey)
102
        ? ResponseEntity.noContent().build()
103
        : ResponseEntity.notFound().build();
104
  }
105
}

Mutations

60

1.1
Location : lambda$createApiKey$0
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:test_create_course_api_key_with_invalid_course_id()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::lambda$createApiKey$0 → KILLED

61

1.1
Location : createApiKey
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:test_create_course_api_key_requires_enable_api_keys_option()]
negated conditional → KILLED

65

1.1
Location : createApiKey
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:test_create_course_api_key_with_label()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::createApiKey → KILLED

79

1.1
Location : lambda$listApiKeys$0
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:list_api_keys_for_unknown_course_is_not_found()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::lambda$listApiKeys$0 → KILLED

80

1.1
Location : listApiKeys
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:list_api_keys_for_course()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::listApiKeys → KILLED

89

1.1
Location : revokeApiKeyById
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:revoke_by_id_success()]
negated conditional → KILLED

92

1.1
Location : revokeApiKeyById
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:revoke_by_id_success()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::revokeApiKeyById → KILLED

101

1.1
Location : revokeApiKey
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:revoke_not_key()]
negated conditional → KILLED

2.2
Location : revokeApiKey
Killed by : edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CourseApiKeyControllerTests]/[method:revoke_not_key()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/CourseApiKeyController::revokeApiKey → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support