Skip to content

Commit

Permalink
Merge pull request #24 from ditsuke/feat/faculty-feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ditsuke committed Apr 12, 2023
2 parents f6ee7b0 + 5a2a74b commit 45f4f78
Show file tree
Hide file tree
Showing 16 changed files with 1,351 additions and 167 deletions.
77 changes: 77 additions & 0 deletions amizone/amizone.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"strings"
"sync"
"text/template"
"time"

"github.com/ditsuke/go-amizone/amizone/internal"
Expand Down Expand Up @@ -37,6 +38,9 @@ const (
// deleteWifiMacEndpoint is peculiar in that it requires the user's ID as a parameter.
// This _might_ open doors for an exploit (spoiler: indeed it does)
removeWifiMacEndpoint = macBaseEndpoint + "/Mac1RegistrationDelete?username=%s&Amizone_Id=%s"

facultyBaseEndpoint = "/FacultyFeeback/FacultyFeedback"
facultyEndpointSubmitEndpoint = facultyBaseEndpoint + "/SaveFeedbackRating"
)

// Miscellaneous
Expand Down Expand Up @@ -434,3 +438,76 @@ func (a *Client) RemoveWifiMac(addr net.HardwareAddr) error {

return nil
}

// SubmitFacultyFeedbackHack submits feedback for *all* faculties, giving the same ratings and comments to all.
// This is a hack because we're not allowing fine-grained control over feedback points or individual faculties. This is
// because the form is a pain to parse, and the feedback system is a pain to work with in general.
// Returns: the number of faculties for which feedback was submitted. Note that this number would be zero
// if the feedback was already submitted or is not open.
func (a *Client) SubmitFacultyFeedbackHack(rating int32, queryRating int32, comment string) (int32, error) {
// Validate
if rating > 5 || rating < 1 {
return 0, errors.New("invalid rating")
}
if queryRating > 3 || queryRating < 1 {
return 0, errors.New("invalid query rating")
}
if comment == "" {
return 0, errors.New("comment cannot be empty")
}

// Transform queryRating for "higher number is higher rating" semantics (it's the opposite in the form 😭)
if queryRating == 1 {
queryRating = 3
} else if queryRating == 3 {
queryRating = 1
}

facultyPage, err := a.doRequest(true, http.MethodGet, facultyBaseEndpoint, nil)
if err != nil {
klog.Errorf("request (faculty page): %s", err.Error())
return 0, fmt.Errorf("%s: %s", ErrFailedToFetchPage, err.Error())
}

feedbackSpecs, err := parse.FacultyFeedback(facultyPage.Body)
if err != nil {
klog.Errorf("parse (faculty feedback): %s", err.Error())
return 0, errors.New(ErrFailedToParsePage)
}

payloadTemplate, err := template.New("facultyFeedback").Parse(facultyFeedbackTpl)
if err != nil {
klog.Errorf("Error parsing faculty feedback template: %s", err.Error())
return 0, errors.New(ErrInternalFailure)
}

// Parallelize feedback submission for max gains 📈
wg := sync.WaitGroup{}
for _, spec := range feedbackSpecs {
spec.Set__Rating = fmt.Sprint(rating)
spec.Set__Comment = url.QueryEscape(comment)
spec.Set__QRating = fmt.Sprint(queryRating)

payload := strings.Builder{}
err = payloadTemplate.Execute(&payload, spec)
if err != nil {
klog.Errorf("Error executing faculty feedback template: %s", err.Error())
return 0, fmt.Errorf("%s: error marshalling feedback request: ", err)
}
klog.Infof("payload for faculty id: %s :: %+v", spec.FacultyId, payload.String())
wg.Add(1)
go func(payload string) {
response, err := a.doRequest(true, http.MethodPost, facultyEndpointSubmitEndpoint, strings.NewReader(payload))
if err != nil {
klog.Errorf("error submitting a faculty feedback: %s", err.Error())
}
if response.StatusCode != http.StatusOK {
klog.Errorf("Non-200 status from faculty feedback submission: %d", response.StatusCode)
}
wg.Done()
}(payload.String())
}

wg.Wait()
return int32(len(feedbackSpecs)), nil
}
12 changes: 12 additions & 0 deletions amizone/internal/mock/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ const (
IDCardPage File = "testdata/id_card_page.html"
WifiPage File = "testdata/wifi_mac_registration.html"
WifiPageOneSlot File = "testdata/wifi_mac_registration_one_empty.html"
FacultyPage File = "testdata/faculty_page.html"
)

type ExpectedJSON string

// Open returns a fs.File interface to the file in filesystem, the mock filesystem.
func (f ExpectedJSON) Open() (fs.File, error) {
return filesystem.Open(string(f))
}

const (
ExpectedFacultyFeedbackSpec ExpectedJSON = "testdata/expected__faculty_feedback_spec.json"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[
{
"VerificationToken": "2oaN2tuDVcdxvIA-H5mN3GMXWywrDHL41076y01r9_TQZGixPRhLfcAEQs9lHxfYh9JJeNLJxKg4rX_6e_vvbxfEymmlsGh2WFc1",
"CourseType": "Open%2FDomain%2FFBL",
"DepartmentId": "94480",
"FacultyId": "121242",
"SerialNumber": "1730207",
"Set__Rating": "",
"Set__QRating": "",
"Set__Comment": ""
},
{
"VerificationToken": "2oaN2tuDVcdxvIA-H5mN3GMXWywrDHL41076y01r9_TQZGixPRhLfcAEQs9lHxfYh9JJeNLJxKg4rX_6e_vvbxfEymmlsGh2WFc1",
"CourseType": "Open%2FDomain%2FFBL",
"DepartmentId": "94480",
"FacultyId": "120530",
"SerialNumber": "1730207",
"Set__Rating": "",
"Set__QRating": "",
"Set__Comment": ""
},
{
"VerificationToken": "2oaN2tuDVcdxvIA-H5mN3GMXWywrDHL41076y01r9_TQZGixPRhLfcAEQs9lHxfYh9JJeNLJxKg4rX_6e_vvbxfEymmlsGh2WFc1",
"CourseType": "Open%2FDomain%2FFBL",
"DepartmentId": "94480",
"FacultyId": "124614",
"SerialNumber": "1730207",
"Set__Rating": "",
"Set__QRating": "",
"Set__Comment": ""
},
{
"VerificationToken": "2oaN2tuDVcdxvIA-H5mN3GMXWywrDHL41076y01r9_TQZGixPRhLfcAEQs9lHxfYh9JJeNLJxKg4rX_6e_vvbxfEymmlsGh2WFc1",
"CourseType": "General",
"DepartmentId": "94480",
"FacultyId": "1248875",
"SerialNumber": "1730207",
"Set__Rating": "",
"Set__QRating": "",
"Set__Comment": ""
},
{
"VerificationToken": "2oaN2tuDVcdxvIA-H5mN3GMXWywrDHL41076y01r9_TQZGixPRhLfcAEQs9lHxfYh9JJeNLJxKg4rX_6e_vvbxfEymmlsGh2WFc1",
"CourseType": "General",
"DepartmentId": "94480",
"FacultyId": "1248913",
"SerialNumber": "1730207",
"Set__Rating": "",
"Set__QRating": "",
"Set__Comment": ""
},
{
"VerificationToken": "2oaN2tuDVcdxvIA-H5mN3GMXWywrDHL41076y01r9_TQZGixPRhLfcAEQs9lHxfYh9JJeNLJxKg4rX_6e_vvbxfEymmlsGh2WFc1",
"CourseType": "General",
"DepartmentId": "94480",
"FacultyId": "1248826",
"SerialNumber": "1730207",
"Set__Rating": "",
"Set__QRating": "",
"Set__Comment": ""
},
{
"VerificationToken": "2oaN2tuDVcdxvIA-H5mN3GMXWywrDHL41076y01r9_TQZGixPRhLfcAEQs9lHxfYh9JJeNLJxKg4rX_6e_vvbxfEymmlsGh2WFc1",
"CourseType": "General",
"DepartmentId": "94480",
"FacultyId": "1249901",
"SerialNumber": "1730207",
"Set__Rating": "",
"Set__QRating": "",
"Set__Comment": ""
}
]
Loading

0 comments on commit 45f4f78

Please sign in to comment.