Skip to content

Commit

Permalink
Changed database to use getCurrrentUser function, tested successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
APandamonium1 committed Jul 26, 2024
1 parent d48788c commit 2c98069
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ func createAnnouncement(announcement Announcement, req *http.Request) error {
func readAnnouncement(announcementID string, req *http.Request) (Announcement, error) {
ref := firebaseClient.NewRef("announcements/" + announcementID)
var announcement Announcement
if err := ref.Get(context.TODO(), &announcementID); err != nil {
if err := ref.Get(context.TODO(), &announcement); err != nil {
return Announcement{}, fmt.Errorf("error reading admin: %v", err)
}

Expand All @@ -708,7 +708,7 @@ func readAnnouncement(announcementID string, req *http.Request) (Announcement, e
return Announcement{}, fmt.Errorf("error getting current user: %v", err)
}

// If user is not admin, return error when attempting to read admin
// If user is not NK user, return error when attempting to read admin
if !isAdmin(currentUser) &&
!isInstructor(currentUser) &&
!isParent(currentUser) &&
Expand Down
43 changes: 37 additions & 6 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@ import (
"net/http/httptest"
"reflect"
"testing"

"encoding/gob"
"encoding/json"
)

func init() {
// Register the User type with gob
gob.Register(User{})
gob.Register(Announcement{})
}

var (
// testStore = sessions.NewCookieStore([]byte("a-very-secret-key"))

currentUser = User{
GoogleID: "admin-user",
Name: "Admin User",
Expand Down Expand Up @@ -82,14 +93,33 @@ var (
}
)

// func mockRequest() *http.Request {
// req, _ := http.NewRequest("GET", "/test", nil)
// session, _ := store.Get(req, "session-name")
// session.Values["user"] = currentUser
// err := session.Save(req, httptest.NewRecorder())
// if err != nil {
// log.Println(err)
// }
// return req
// }

func mockRequest() *http.Request {
req, _ := http.NewRequest("GET", "/test", nil)
session, _ := store.Get(req, "session-name")
session.Values["user"] = currentUser
err := session.Save(req, httptest.NewRecorder())
recorder := httptest.NewRecorder()
session, _ := store.Get(req, "auth-session")

userData, err := json.Marshal(currentUser)
if err != nil {
log.Println(err)
}

session.Values["user"] = userData
err = session.Save(req, recorder)
if err != nil {
log.Println(err)
}
req.Header.Set("Cookie", recorder.Header().Get("Set-Cookie"))
return req
}

Expand Down Expand Up @@ -143,7 +173,8 @@ func TestReadStudent(t *testing.T) {
func TestUpdateStudent(t *testing.T) {
// Update the student's email
updates := map[string]interface{}{
"name": "Updated Student",
"name": "John Doe",
"contact_number": "99999999",
}

err := updateStudent(students[0].GoogleID, updates, mockRequest())
Expand All @@ -158,7 +189,7 @@ func TestUpdateStudent(t *testing.T) {
}

// Assert that the updated student's email is correct
if readStudent.Name != updates["name"] {
if students[0].Name != updates["name"] {
t.Errorf("Updated student's name is incorrect. Expected: %v, Got: %v", updates["name"], readStudent.Name)
}
}
Expand Down Expand Up @@ -461,7 +492,7 @@ func TestCreateAnnouncement(t *testing.T) {

// Assert that the created and read announcement are equal
if !reflect.DeepEqual(announcement, readAnnouncement) {
t.Error("Created and read announcements are not equal")
t.Errorf("Created and read announcements are not equal. Expected: %+v, Got: %+v", announcement, readAnnouncement)
}
}

Expand Down

0 comments on commit 2c98069

Please sign in to comment.