CourseAccessService.java

1
package edu.ucsb.cs156.happiercows.services;
2
3
import edu.ucsb.cs156.happiercows.entities.Game;
4
import edu.ucsb.cs156.happiercows.entities.Staff;
5
import edu.ucsb.cs156.happiercows.entities.Student;
6
import edu.ucsb.cs156.happiercows.entities.User;
7
import edu.ucsb.cs156.happiercows.repositories.StaffRepository;
8
import edu.ucsb.cs156.happiercows.repositories.StudentRepository;
9
import edu.ucsb.cs156.happiercows.utilities.CanonicalFormConverter;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.stereotype.Service;
12
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.Optional;
16
17
/**
18
 * Determines whether a user has access to a course-linked Game, based on
19
 * whether their email appears on the roster (as a Student or Staff member)
20
 * of the course the Game is linked to. See issue #251.
21
 */
22
@Service
23
public class CourseAccessService {
24
25
    @Autowired
26
    private StudentRepository studentRepository;
27
28
    @Autowired
29
    private StaffRepository staffRepository;
30
31
    /**
32
     * A user is eligible for a course-linked game if they are an admin, or
33
     * if their email appears on the course's roster of students or staff.
34
     * A game with no course (courseId == null) is open to everyone.
35
     */
36
    public boolean isEligibleForGame(User user, Game game) {
37 1 1. isEligibleForGame : negated conditional → KILLED
        if (game.getCourseId() == null) {
38 1 1. isEligibleForGame : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED
            return true;
39
        }
40 1 1. isEligibleForGame : negated conditional → KILLED
        if (user.isAdmin()) {
41 1 1. isEligibleForGame : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED
            return true;
42
        }
43 2 1. isEligibleForGame : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED
2. isEligibleForGame : replaced boolean return with true for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED
        return getCourseIdsForUser(user).contains(game.getCourseId());
44
    }
45
46
    /**
47
     * Returns the distinct list of course ids for which this user's email
48
     * appears on the roster as a student or as staff.
49
     *
50
     * <p>Roster emails are compared to the user's email using
51
     * {@link CanonicalFormConverter#areEquivalentEmails}, not exact string
52
     * equality, so that a roster row uploaded with a stale/non-canonical
53
     * email (e.g. an old {@code @umail.ucsb.edu} entry that predates this
54
     * fix, or one that slips in from a school whose export format isn't
55
     * fully normalized) still matches a student logging in with the
56
     * canonical form of the same address. See issue #278.
57
     */
58
    public List<Long> getCourseIdsForUser(User user) {
59
        List<Long> courseIds = new ArrayList<>();
60
        String email = user.getEmail();
61
62
        for (Student student : studentRepository.findAll()) {
63 1 1. getCourseIdsForUser : negated conditional → KILLED
            if (student.getCourseId() != null
64 1 1. getCourseIdsForUser : negated conditional → KILLED
                    && !courseIds.contains(student.getCourseId())
65 1 1. getCourseIdsForUser : negated conditional → KILLED
                    && CanonicalFormConverter.areEquivalentEmails(student.getEmail(), email)) {
66
                courseIds.add(student.getCourseId());
67
            }
68
        }
69
70
        for (Staff staff : staffRepository.findAll()) {
71 1 1. getCourseIdsForUser : negated conditional → KILLED
            if (staff.getCourseId() != null
72 1 1. getCourseIdsForUser : negated conditional → KILLED
                    && !courseIds.contains(staff.getCourseId())
73 1 1. getCourseIdsForUser : negated conditional → KILLED
                    && CanonicalFormConverter.areEquivalentEmails(staff.getEmail(), email)) {
74
                courseIds.add(staff.getCourseId());
75
            }
76
        }
77
78 1 1. getCourseIdsForUser : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/services/CourseAccessService::getCourseIdsForUser → KILLED
        return courseIds;
79
    }
80
81
    /**
82
     * Returns the Student roster row that corresponds to this user, for the
83
     * course linked to the given game - but only if the game is actually
84
     * linked to a course. Used to decide whether a FarmerActivity row should
85
     * be recorded for a play-page view or a buy/sell action: activity is
86
     * only tracked for course-linked games, and only for users whose email
87
     * matches a student on that course's roster (see issue #291), using the
88
     * same canonical-email-matching rule as {@link #getCourseIdsForUser}.
89
     */
90
    public Optional<Student> findMatchingStudentForCourseLinkedGame(User user, Game game) {
91 1 1. findMatchingStudentForCourseLinkedGame : negated conditional → KILLED
        if (game.getCourseId() == null) {
92
            return Optional.empty();
93
        }
94
95
        String email = user.getEmail();
96
97
        for (Student student : studentRepository.findByCourseId(game.getCourseId())) {
98 1 1. findMatchingStudentForCourseLinkedGame : negated conditional → KILLED
            if (CanonicalFormConverter.areEquivalentEmails(student.getEmail(), email)) {
99 1 1. findMatchingStudentForCourseLinkedGame : replaced return value with Optional.empty for edu/ucsb/cs156/happiercows/services/CourseAccessService::findMatchingStudentForCourseLinkedGame → KILLED
                return Optional.of(student);
100
            }
101
        }
102
103
        return Optional.empty();
104
    }
105
}

Mutations

37

1.1
Location : isEligibleForGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:game_with_no_course_is_open_to_everyone()]
negated conditional → KILLED

38

1.1
Location : isEligibleForGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:game_with_no_course_is_open_to_everyone()]
replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED

40

1.1
Location : isEligibleForGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:user_not_on_roster_is_not_eligible()]
negated conditional → KILLED

41

1.1
Location : isEligibleForGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:admin_is_always_eligible_for_a_course_linked_game()]
replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED

43

1.1
Location : isEligibleForGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:student_on_roster_is_eligible()]
replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED

2.2
Location : isEligibleForGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:user_not_on_roster_is_not_eligible()]
replaced boolean return with true for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED

63

1.1
Location : getCourseIdsForUser
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:getCourseIdsForUser_returns_distinct_union_of_student_and_staff_courses()]
negated conditional → KILLED

64

1.1
Location : getCourseIdsForUser
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:getCourseIdsForUser_returns_distinct_union_of_student_and_staff_courses()]
negated conditional → KILLED

65

1.1
Location : getCourseIdsForUser
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:getCourseIdsForUser_does_not_match_unrelated_emails()]
negated conditional → KILLED

71

1.1
Location : getCourseIdsForUser
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:staff_on_roster_with_umail_domain_matches_user_logging_in_with_ucsb_domain()]
negated conditional → KILLED

72

1.1
Location : getCourseIdsForUser
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:getCourseIdsForUser_returns_distinct_union_of_student_and_staff_courses()]
negated conditional → KILLED

73

1.1
Location : getCourseIdsForUser
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:getCourseIdsForUser_does_not_match_unrelated_emails()]
negated conditional → KILLED

78

1.1
Location : getCourseIdsForUser
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:getCourseIdsForUser_returns_distinct_union_of_student_and_staff_courses()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/services/CourseAccessService::getCourseIdsForUser → KILLED

91

1.1
Location : findMatchingStudentForCourseLinkedGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:findMatchingStudentForCourseLinkedGame_returns_matching_student()]
negated conditional → KILLED

98

1.1
Location : findMatchingStudentForCourseLinkedGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:findMatchingStudentForCourseLinkedGame_returns_matching_student()]
negated conditional → KILLED

99

1.1
Location : findMatchingStudentForCourseLinkedGame
Killed by : edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.CourseAccessServiceTests]/[method:findMatchingStudentForCourseLinkedGame_returns_matching_student()]
replaced return value with Optional.empty for edu/ucsb/cs156/happiercows/services/CourseAccessService::findMatchingStudentForCourseLinkedGame → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1