Skip to content

Commit

Permalink
Reset the API locator numbers (now that the API has stabalized).
Browse files Browse the repository at this point in the history
  • Loading branch information
eriq-augustine committed Dec 30, 2023
1 parent fde7b07 commit 7767da9
Show file tree
Hide file tree
Showing 33 changed files with 124 additions and 124 deletions.
8 changes: 4 additions & 4 deletions api/admin/update_course.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ func HandleUpdateCourse(request *UpdateCourseRequest) (*UpdateCourseResponse, *c
if (request.Clear) {
err := db.ClearCourse(request.Course);
if (err != nil) {
return nil, core.NewInternalError("-701", &request.APIRequestCourseUserContext,
return nil, core.NewInternalError("-201", &request.APIRequestCourseUserContext,
"Failed to clear course.").Err(err);
}
}

if (request.Source != "") {
spec, err := common.ParseFileSpec(request.Source);
if (err != nil) {
return nil, core.NewBadCourseRequestError("-702", &request.APIRequestCourseUserContext,
return nil, core.NewBadCourseRequestError("-202", &request.APIRequestCourseUserContext,
"Source FileSpec is not formatted properly.").Err(err);
}

request.Course.Source = spec;

err = db.SaveCourse(request.Course);
if (err != nil) {
return nil, core.NewInternalError("-703", &request.APIRequestCourseUserContext,
return nil, core.NewInternalError("-203", &request.APIRequestCourseUserContext,
"Failed to save course.").Err(err);
}
}

updated, err := procedures.UpdateCourse(request.Course);
if (err != nil) {
return nil, core.NewInternalError("-704", &request.APIRequestCourseUserContext,
return nil, core.NewInternalError("-204", &request.APIRequestCourseUserContext,
"Failed to update course.").Err(err);
}

Expand Down
6 changes: 3 additions & 3 deletions api/core/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
func (this *APIRequestCourseUserContext) Auth() (*model.User, *APIError) {
user, err := db.GetUser(this.Course, this.UserEmail);
if (err != nil) {
return nil, NewAuthBadRequestError("-201", this, "Cannot Get User").Err(err);
return nil, NewAuthBadRequestError("-012", this, "Cannot Get User").Err(err);
}

if (user == nil) {
return nil, NewAuthBadRequestError("-202", this, "Unknown User");
return nil, NewAuthBadRequestError("-013", this, "Unknown User");
}

if (config.NO_AUTH.Get()) {
Expand All @@ -27,7 +27,7 @@ func (this *APIRequestCourseUserContext) Auth() (*model.User, *APIError) {
}

if (!user.CheckPassword(this.UserPass)) {
return nil, NewAuthBadRequestError("-203", this, "Bad Password");
return nil, NewAuthBadRequestError("-014", this, "Bad Password");
}

return user, nil;
Expand Down
22 changes: 11 additions & 11 deletions api/core/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ func TestAuth(test *testing.T) {
{"[email protected]", "student", false, ""},
{"[email protected]", "other", false, ""},

{"Z", "student", false, "-202"},
{"[email protected]", "student", false, "-202"},
{"[email protected]", "student", false, "-202"},
{"student", "student", false, "-202"},
{"Z", "student", false, "-013"},
{"[email protected]", "student", false, "-013"},
{"[email protected]", "student", false, "-013"},
{"student", "student", false, "-013"},

{"[email protected]", "", false, "-203"},
{"[email protected]", "Zstudent", false, "-203"},
{"[email protected]", "studentZ", false, "-203"},
{"[email protected]", "", false, "-014"},
{"[email protected]", "Zstudent", false, "-014"},
{"[email protected]", "studentZ", false, "-014"},

{"[email protected]", "owner", true, ""},
{"[email protected]", "admin", true, ""},
{"[email protected]", "grader", true, ""},
{"[email protected]", "student", true, ""},
{"[email protected]", "other", true, ""},

{"Z", "student", true, "-202"},
{"[email protected]", "student", true, "-202"},
{"[email protected]", "student", true, "-202"},
{"student", "student", true, "-202"},
{"Z", "student", true, "-013"},
{"[email protected]", "student", true, "-013"},
{"[email protected]", "student", true, "-013"},
{"student", "student", true, "-013"},

{"[email protected]", "", true, ""},
{"[email protected]", "Zstudent", true, ""},
Expand Down
24 changes: 12 additions & 12 deletions api/core/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ func (this *APIRequestCourseUserContext) Validate(request any, endpoint string)
}

if (this.CourseID == "") {
return NewBadRequestError("-301", &this.APIRequest, "No course ID specified.");
return NewBadRequestError("-015", &this.APIRequest, "No course ID specified.");
}

if (this.UserEmail == "") {
return NewBadRequestError("-302", &this.APIRequest, "No user email specified.");
return NewBadRequestError("-016", &this.APIRequest, "No user email specified.");
}

if (this.UserPass == "") {
return NewBadRequestError("-303", &this.APIRequest, "No user password specified.");
return NewBadRequestError("-017", &this.APIRequest, "No user password specified.");
}

var err error;
this.Course, err = db.GetCourse(this.CourseID);
if (err != nil) {
return NewInternalError("-318", this, "Unable to get course").Err(err);
return NewInternalError("-032", this, "Unable to get course").Err(err);
}

if (this.Course == nil) {
return NewBadRequestError("-304", &this.APIRequest, fmt.Sprintf("Could not find course: '%s'.", this.CourseID)).
return NewBadRequestError("-018", &this.APIRequest, fmt.Sprintf("Could not find course: '%s'.", this.CourseID)).
Add("course-id", this.CourseID);
}

Expand All @@ -100,11 +100,11 @@ func (this *APIRequestCourseUserContext) Validate(request any, endpoint string)

minRole, foundRole := getMaxRole(request);
if (!foundRole) {
return NewInternalError("-305", this, "No role found for request. All request structs require a minimum role.");
return NewInternalError("-019", this, "No role found for request. All request structs require a minimum role.");
}

if (this.User.Role < minRole) {
return NewBadPermissionsError("-306", this, minRole, "Base API Request");
return NewBadPermissionsError("-020", this, minRole, "Base API Request");
}

return nil;
Expand All @@ -118,12 +118,12 @@ func (this *APIRequestAssignmentContext) Validate(request any, endpoint string)
}

if (this.AssignmentID == "") {
return NewBadRequestError("-307", &this.APIRequest, "No assignment ID specified.");
return NewBadRequestError("-021", &this.APIRequest, "No assignment ID specified.");
}

this.Assignment = this.Course.GetAssignment(this.AssignmentID);
if (this.Assignment == nil) {
return NewBadRequestError("-308", &this.APIRequest, fmt.Sprintf("Could not find assignment: '%s'.", this.AssignmentID)).
return NewBadRequestError("-022", &this.APIRequest, fmt.Sprintf("Could not find assignment: '%s'.", this.AssignmentID)).
Add("course-id", this.CourseID).Add("assignment-id", this.AssignmentID);
}

Expand All @@ -135,7 +135,7 @@ func (this *APIRequestAssignmentContext) Validate(request any, endpoint string)
func ValidateAPIRequest(request *http.Request, apiRequest any, endpoint string) *APIError {
reflectPointer := reflect.ValueOf(apiRequest);
if (reflectPointer.Kind() != reflect.Pointer) {
return NewBareInternalError("-309", endpoint, "ValidateAPIRequest() must be called with a pointer.").
return NewBareInternalError("-023", endpoint, "ValidateAPIRequest() must be called with a pointer.").
Add("kind", reflectPointer.Kind().String());
}

Expand All @@ -146,7 +146,7 @@ func ValidateAPIRequest(request *http.Request, apiRequest any, endpoint string)
}

if (!foundRequestStruct) {
return NewBareInternalError("-310", endpoint, "Request is not any kind of known API request.");
return NewBareInternalError("-024", endpoint, "Request is not any kind of known API request.");
}

// Check for any special field types that we know how to populate.
Expand Down Expand Up @@ -185,7 +185,7 @@ func validateRequestStruct(request any, endpoint string) (bool, *APIError) {

reflectValue := reflect.ValueOf(request).Elem();
if (reflectValue.Kind() != reflect.Struct) {
return false, NewBareInternalError("-317", endpoint, "Request's type must be a struct.").
return false, NewBareInternalError("-031", endpoint, "Request's type must be a struct.").
Add("kind", reflectValue.Kind().String());
}

Expand Down
18 changes: 9 additions & 9 deletions api/core/request_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func checkRequestTargetUser(endpoint string, apiRequest any, fieldIndex int) *AP
jsonName := util.JSONFieldName(fieldType);

if (field.Email == "") {
return NewBadRequestError("-320", &courseContext.APIRequest,
return NewBadRequestError("-034", &courseContext.APIRequest,
fmt.Sprintf("Field '%s' requires a non-empty string, empty or null provided.", jsonName)).
Add("struct-name", structName).Add("field-name", fieldType.Name).Add("json-name", jsonName);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func checkRequestTargetUserSelfOrRole(endpoint string, apiRequest any, fieldInde

// Operations not on self require higher permissions.
if ((field.Email != courseContext.User.Email) && (courseContext.User.Role < minRole)) {
return NewBadPermissionsError("-319", courseContext, minRole, "Non-Self Target User");
return NewBadPermissionsError("-033", courseContext, minRole, "Non-Self Target User");
}

user := users[field.Email];
Expand All @@ -198,19 +198,19 @@ func checkRequestPostFiles(request *http.Request, endpoint string, apiRequest an
fieldType := reflectValue.Type().Field(fieldIndex);

if (!fieldType.IsExported()) {
return NewBareInternalError("-314", endpoint, "A POSTFiles field must be exported.").
return NewBareInternalError("-028", endpoint, "A POSTFiles field must be exported.").
Add("struct-name", structName).Add("field-name", fieldType.Name);
}

postFiles, err := storeRequestFiles(request);

if (err != nil) {
return NewBareInternalError("-315", endpoint, "Failed to store files from POST.").Err(err).
return NewBareInternalError("-029", endpoint, "Failed to store files from POST.").Err(err).
Add("struct-name", structName).Add("field-name", fieldType.Name);
}

if (postFiles == nil) {
return NewBareBadRequestError("-316", endpoint, "Endpoint requires files to be provided in POST body, no files found.").
return NewBareBadRequestError("-030", endpoint, "Endpoint requires files to be provided in POST body, no files found.").
Add("struct-name", structName).Add("field-name", fieldType.Name);
}

Expand All @@ -230,7 +230,7 @@ func checkRequestNonEmptyString(endpoint string, apiRequest any, fieldIndex int)

value := fieldValue.Interface().(NonEmptyString);
if (value == "") {
return NewBareBadRequestError("-318", endpoint,
return NewBareBadRequestError("-032", endpoint,
fmt.Sprintf("Field '%s' requires a non-empty string, empty or null provided.", jsonName)).
Add("struct-name", structName).Add("field-name", fieldType.Name).Add("json-name", jsonName);
}
Expand Down Expand Up @@ -327,22 +327,22 @@ func baseCheckRequestUsersField(endpoint string, apiRequest any, fieldIndex int)
courseContextValue := reflectValue.FieldByName("APIRequestCourseUserContext");
if (!courseContextValue.IsValid() || courseContextValue.IsZero()) {
return nil, nil,
NewBareInternalError("-311", endpoint, "A request with type requiring users must embed APIRequestCourseUserContext").
NewBareInternalError("-025", endpoint, "A request with type requiring users must embed APIRequestCourseUserContext").
Add("request", apiRequest).
Add("struct-name", structName).Add("field-name", fieldType.Name).Add("field-type", fieldName);
}
courseContext := courseContextValue.Interface().(APIRequestCourseUserContext);

if (!fieldType.IsExported()) {
return nil, nil,
NewInternalError("-312", &courseContext, "Field must be exported.").
NewInternalError("-026", &courseContext, "Field must be exported.").
Add("struct-name", structName).Add("field-name", fieldType.Name).Add("field-type", fieldName);
}

users, err := db.GetUsers(courseContext.Course);
if (err != nil) {
return nil, nil,
NewInternalError("-313", &courseContext, "Failed to fetch embeded users.").Err(err).
NewInternalError("-027", &courseContext, "Failed to fetch embeded users.").Err(err).
Add("struct-name", structName).Add("field-name", fieldType.Name).Add("field-type", fieldName);
}

Expand Down
30 changes: 15 additions & 15 deletions api/core/request_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestBadUsersFieldNoContext(test *testing.T) {
i, testCase.request);
}

if (apiErr.Locator != "-311") {
test.Fatalf("Case %d: Struct with no course context does not return an error with locator '-311', found '%s': '%+v.",
if (apiErr.Locator != "-025") {
test.Fatalf("Case %d: Struct with no course context does not return an error with locator '-025', found '%s': '%+v.",
i, apiErr.Locator, testCase.request);
}
}
Expand Down Expand Up @@ -78,8 +78,8 @@ func TestBadUsersFieldNotExported(test *testing.T) {
i, testCase.request);
}

if (apiErr.Locator != "-312") {
test.Fatalf("Case %d: Struct with non-exported field does not return an error with locator '-312', found '%s': '%v.",
if (apiErr.Locator != "-026") {
test.Fatalf("Case %d: Struct with non-exported field does not return an error with locator '-026', found '%s': '%v.",
i, apiErr.Locator, apiErr);
}
}
Expand All @@ -91,13 +91,13 @@ func TestNonEmptyStringField(test *testing.T) {

{&struct{ APIRequest; Text NonEmptyString }{Text: "ZZZ"}, "", "Text"},

{&struct{ APIRequest; Text NonEmptyString }{}, "-318", "Text"},
{&struct{ APIRequest; Text NonEmptyString }{Text: ""}, "-318", "Text"},
{&struct{ APIRequest; Text NonEmptyString }{}, "-032", "Text"},
{&struct{ APIRequest; Text NonEmptyString }{Text: ""}, "-032", "Text"},

{&struct{ APIRequest; Text NonEmptyString `json:"text"`}{}, "-318", "text"},
{&struct{ APIRequest; Text NonEmptyString `json:"text,omitempty"`}{}, "-318", "text"},
{&struct{ APIRequest; Text NonEmptyString `json:"foo-bar"`}{}, "-318", "foo-bar"},
{&struct{ APIRequest; Text NonEmptyString `json:"foo-bar,omitempty"`}{}, "-318", "foo-bar"},
{&struct{ APIRequest; Text NonEmptyString `json:"text"`}{}, "-032", "text"},
{&struct{ APIRequest; Text NonEmptyString `json:"text,omitempty"`}{}, "-032", "text"},
{&struct{ APIRequest; Text NonEmptyString `json:"foo-bar"`}{}, "-032", "foo-bar"},
{&struct{ APIRequest; Text NonEmptyString `json:"foo-bar,omitempty"`}{}, "-032", "foo-bar"},
};

for i, testCase := range testCases {
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestBadPostFilesFieldNotExported(test *testing.T) {
test.Fatalf("Struct with non-exported files does not return an error,");
}

expectedLocator := "-314";
expectedLocator := "-028";
if (apiErr.Locator != expectedLocator) {
test.Fatalf("Struct with non-exported files does not return an error with the correct locator. Expcted '%s', found '%s'.",
expectedLocator, apiErr.Locator);
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestBadPostFilesNoFiles(test *testing.T) {
test.Fatalf("Request did not generate an error: '%v'.", response);
}

expectedLocator := "-316";
expectedLocator := "-030";
if (response.Locator != expectedLocator) {
test.Fatalf("Error does not have the correct locator. Expcted '%s', found '%s'.",
expectedLocator, response.Locator);
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestBadPostFilesStoreFail(test *testing.T) {
test.Fatalf("Request did not generate an error: '%v'.", response);
}

expectedLocator := "-315";
expectedLocator := "-029";
if (response.Locator != expectedLocator) {
test.Fatalf("Error does not have the correct locator. Expcted '%s', found '%s'.",
expectedLocator, response.Locator);
Expand Down Expand Up @@ -453,7 +453,7 @@ func testTargetUser[T comparable, V userGetter](test *testing.T,
apiErr := ValidateAPIRequest(nil, request, "");
if (apiErr != nil) {
if (testCase.permError) {
expectedLocator := "-319";
expectedLocator := "-033";
if (expectedLocator != apiErr.Locator) {
test.Errorf("Case %d: Incorrect error returned on permissions error. Expcted '%s', found '%s'.",
i, expectedLocator, apiErr.Locator);
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestTargetUser(test *testing.T) {
apiErr := ValidateAPIRequest(nil, &request, "");
if (apiErr != nil) {
if (testCase.target == "") {
expectedLocator := "-320";
expectedLocator := "-034";
if (expectedLocator != apiErr.Locator) {
test.Errorf("Case %d: Incorrect error returned on empty string. Expcted '%s', found '%s'.",
i, expectedLocator, apiErr.Locator);
Expand Down
Loading

0 comments on commit 7767da9

Please sign in to comment.