From 980789d0f5eb02752452fb4d67241f2bd5a279a2 Mon Sep 17 00:00:00 2001 From: beebeeoii Date: Sat, 22 Jan 2022 15:55:15 +0800 Subject: [PATCH] implemented module/grades response validation --- pkg/api/grades.go | 19 +++++++++++++++++++ pkg/api/modules.go | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/pkg/api/grades.go b/pkg/api/grades.go index b4cc9a6..6133965 100644 --- a/pkg/api/grades.go +++ b/pkg/api/grades.go @@ -16,6 +16,18 @@ type Grade struct { const GRADE_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/gradebook/?populate=scores&ParentID=%s" +// getGradeFieldsRequired is a helper function that returns a constant array with fields that a Grade response +// returned by Luminus needs. +func getGradeFieldsRequired() []string { + return []string{"access", "scores", "name", "maxMark"} +} + +// getScoreDetailFieldsRequired is a helper function that returns a constant array with fields that a Grade["scores"] element +// returned by Luminus needs. +func getScoreDetailFieldsRequired() []string { + return []string{"finalMark", "remark", "lastUpdatedDate", "maxMark"} +} + // GetGrades retrieves all grades for a particular module represented by moduleCode specified in GradeRequest. // Find out more about GradeRequests under request.go. func (req GradeRequest) GetGrades() ([]Grade, error) { @@ -28,10 +40,17 @@ func (req GradeRequest) GetGrades() ([]Grade, error) { } for _, content := range rawResponse.Data { + if !IsResponseValid(getGradeFieldsRequired(), content) { + continue + } + if _, exists := content["access"]; exists { // only grades that can be accessed will be placed in grades slice scoreDetail := make(map[string]interface{}) if len(content["scores"].([]interface{})) > 0 { scoreDetail = (content["scores"].([]interface{})[0]).(map[string]interface{}) + if !IsResponseValid(getScoreDetailFieldsRequired(), scoreDetail) { + continue + } } testName := content["name"].(string) mark := -1.0 diff --git a/pkg/api/modules.go b/pkg/api/modules.go index b58f95c..3257276 100644 --- a/pkg/api/modules.go +++ b/pkg/api/modules.go @@ -15,6 +15,18 @@ type Module struct { const MODULE_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/module/?populate=Creator,termDetail,isMandatory" +// getModuleFieldsRequired is a helper function that returns a constant array with fields that a Module response +// returned by Luminus needs. +func getModuleFieldsRequired() []string { + return []string{"access", "termDetail", "courseName", "name", "creatorName", "creatorEmail"} +} + +// getTermDetailFieldsRequired is a helper function that returns a constant array with fields that a Module["termDetail"] +// returned by Luminus needs. +func getTermDetailFieldsRequired() []string { + return []string{"description"} +} + // GetModules retrieves all modules that are taken by the user using a ModuleRequest. // Find out more about ModuleRequests under request.go. func (req ModuleRequest) GetModules() ([]Module, error) { @@ -27,9 +39,17 @@ func (req ModuleRequest) GetModules() ([]Module, error) { } for _, content := range rawResponse.Data { + if !IsResponseValid(getModuleFieldsRequired(), content) { + continue + } + _, accessible := content["access"] if accessible { termDetail := content["termDetail"].(map[string]interface{}) + if !IsResponseValid(getTermDetailFieldsRequired(), termDetail) { + continue + } + module := Module{ Id: content["id"].(string), Name: content["courseName"].(string),