|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ayush00git/cms-web/models" |
| 8 | +) |
| 9 | + |
| 10 | +// --- FacultyProfileEdit ------------------------------------------------------ |
| 11 | + |
| 12 | +func TestFacultyProfileEdit_Success(t *testing.T) { |
| 13 | + db := newTestDB(t) |
| 14 | + f := seedFaculty(t, db, "f.edit@iit.ac.in") |
| 15 | + |
| 16 | + e := newAuthRouter(db, authAs(f.ID, f.Email)) |
| 17 | + rec := doRequest(t, e, http.MethodPatch, "/api/faculty/profile/edit", map[string]any{ |
| 18 | + "name": "Updated Name", |
| 19 | + "department": string(models.ECE), |
| 20 | + "house_number": "42", |
| 21 | + "block": string(models.BlockB), |
| 22 | + "type": string(models.Type2), |
| 23 | + "phone_number": "6666666666", |
| 24 | + }) |
| 25 | + |
| 26 | + assertStatus(t, rec, 200) |
| 27 | + |
| 28 | + var updated models.Faculty |
| 29 | + db.First(&updated, f.ID) |
| 30 | + if updated.Name != "Updated Name" || updated.Department != models.ECE || |
| 31 | + updated.HouseNumber != "42" || updated.Block != models.BlockB || |
| 32 | + updated.Type != models.Type2 || updated.PhoneNumber != "6666666666" { |
| 33 | + t.Fatalf("profile fields not updated as expected: %+v", updated) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestFacultyProfileEdit_PartialUpdate(t *testing.T) { |
| 38 | + db := newTestDB(t) |
| 39 | + f := seedFaculty(t, db, "f.partial@iit.ac.in") |
| 40 | + |
| 41 | + e := newAuthRouter(db, authAs(f.ID, f.Email)) |
| 42 | + rec := doRequest(t, e, http.MethodPatch, "/api/faculty/profile/edit", map[string]any{ |
| 43 | + "phone_number": "5555555555", |
| 44 | + }) |
| 45 | + |
| 46 | + assertStatus(t, rec, 200) |
| 47 | + |
| 48 | + var updated models.Faculty |
| 49 | + db.First(&updated, f.ID) |
| 50 | + if updated.PhoneNumber != "5555555555" { |
| 51 | + t.Fatalf("expected phone number updated, got %q", updated.PhoneNumber) |
| 52 | + } |
| 53 | + if updated.Department != f.Department || updated.Name != f.Name { |
| 54 | + t.Fatalf("expected untouched fields to survive a partial update, got %+v", updated) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestFacultyProfileEdit_Unauthenticated(t *testing.T) { |
| 59 | + db := newTestDB(t) |
| 60 | + e := newAuthRouter(db, noAuth()) |
| 61 | + rec := doRequest(t, e, http.MethodPatch, "/api/faculty/profile/edit", map[string]any{"name": "x"}) |
| 62 | + assertStatus(t, rec, 403) |
| 63 | +} |
| 64 | + |
| 65 | +func TestFacultyProfileEdit_ProfileNotFound(t *testing.T) { |
| 66 | + db := newTestDB(t) |
| 67 | + e := newAuthRouter(db, authAs(1, "ghost@iit.ac.in")) |
| 68 | + rec := doRequest(t, e, http.MethodPatch, "/api/faculty/profile/edit", map[string]any{"name": "x"}) |
| 69 | + assertStatus(t, rec, 404) |
| 70 | +} |
| 71 | + |
| 72 | +func TestFacultyProfileEdit_InvalidBody(t *testing.T) { |
| 73 | + db := newTestDB(t) |
| 74 | + f := seedFaculty(t, db, "f.badbody@iit.ac.in") |
| 75 | + e := newAuthRouter(db, authAs(f.ID, f.Email)) |
| 76 | + rec := doRequest(t, e, http.MethodPatch, "/api/faculty/profile/edit", []string{"bad"}) |
| 77 | + assertStatus(t, rec, 400) |
| 78 | +} |
| 79 | + |
| 80 | +// --- WardenProfileEdit ------------------------------------------------------- |
| 81 | + |
| 82 | +func TestWardenProfileEdit_Success(t *testing.T) { |
| 83 | + db := newTestDB(t) |
| 84 | + w := seedWarden(t, db, "w.edit@iit.ac.in") |
| 85 | + |
| 86 | + e := newAuthRouter(db, authAs(w.ID, w.Email)) |
| 87 | + rec := doRequest(t, e, http.MethodPatch, "/api/warden/profile/edit", map[string]any{ |
| 88 | + "name": "Updated Warden", |
| 89 | + "hostel": string(models.HBH), |
| 90 | + "phone_number": "4444444444", |
| 91 | + }) |
| 92 | + |
| 93 | + assertStatus(t, rec, 200) |
| 94 | + |
| 95 | + var updated models.Warden |
| 96 | + db.First(&updated, w.ID) |
| 97 | + if updated.Name != "Updated Warden" || updated.Hostel != models.HBH || updated.PhoneNumber != "4444444444" { |
| 98 | + t.Fatalf("profile fields not updated as expected: %+v", updated) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestWardenProfileEdit_PartialUpdate(t *testing.T) { |
| 103 | + db := newTestDB(t) |
| 104 | + w := seedWarden(t, db, "w.partial@iit.ac.in") |
| 105 | + |
| 106 | + e := newAuthRouter(db, authAs(w.ID, w.Email)) |
| 107 | + rec := doRequest(t, e, http.MethodPatch, "/api/warden/profile/edit", map[string]any{ |
| 108 | + "hostel": string(models.NBH), |
| 109 | + }) |
| 110 | + |
| 111 | + assertStatus(t, rec, 200) |
| 112 | + |
| 113 | + var updated models.Warden |
| 114 | + db.First(&updated, w.ID) |
| 115 | + if updated.Hostel != models.NBH { |
| 116 | + t.Fatalf("expected hostel updated, got %q", updated.Hostel) |
| 117 | + } |
| 118 | + if updated.PhoneNumber != w.PhoneNumber { |
| 119 | + t.Fatalf("expected untouched fields to survive a partial update, got %+v", updated) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestWardenProfileEdit_Unauthenticated(t *testing.T) { |
| 124 | + db := newTestDB(t) |
| 125 | + e := newAuthRouter(db, noAuth()) |
| 126 | + rec := doRequest(t, e, http.MethodPatch, "/api/warden/profile/edit", map[string]any{"name": "x"}) |
| 127 | + assertStatus(t, rec, 403) |
| 128 | +} |
| 129 | + |
| 130 | +func TestWardenProfileEdit_ProfileNotFound(t *testing.T) { |
| 131 | + db := newTestDB(t) |
| 132 | + e := newAuthRouter(db, authAs(1, "ghost@iit.ac.in")) |
| 133 | + rec := doRequest(t, e, http.MethodPatch, "/api/warden/profile/edit", map[string]any{"name": "x"}) |
| 134 | + assertStatus(t, rec, 404) |
| 135 | +} |
| 136 | + |
| 137 | +func TestWardenProfileEdit_InvalidBody(t *testing.T) { |
| 138 | + db := newTestDB(t) |
| 139 | + w := seedWarden(t, db, "w.badbody@iit.ac.in") |
| 140 | + e := newAuthRouter(db, authAs(w.ID, w.Email)) |
| 141 | + rec := doRequest(t, e, http.MethodPatch, "/api/warden/profile/edit", []string{"bad"}) |
| 142 | + assertStatus(t, rec, 400) |
| 143 | +} |
| 144 | + |
| 145 | +// --- CentreheadProfileEdit --------------------------------------------------- |
| 146 | + |
| 147 | +func TestCentreheadProfileEdit_Success(t *testing.T) { |
| 148 | + db := newTestDB(t) |
| 149 | + ch := seedCentrehead(t, db, "ch.edit@iit.ac.in") |
| 150 | + |
| 151 | + e := newAuthRouter(db, authAs(ch.ID, ch.Email)) |
| 152 | + rec := doRequest(t, e, http.MethodPatch, "/api/centrehead/profile/edit", map[string]any{ |
| 153 | + "building": string(models.CentralLibrary), |
| 154 | + "phone_number": "3333333333", |
| 155 | + }) |
| 156 | + |
| 157 | + assertStatus(t, rec, 200) |
| 158 | + |
| 159 | + var updated models.Centrehead |
| 160 | + db.First(&updated, ch.ID) |
| 161 | + if updated.Building != models.CentralLibrary || updated.PhoneNumber != "3333333333" { |
| 162 | + t.Fatalf("profile fields not updated as expected: %+v", updated) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func TestCentreheadProfileEdit_PartialUpdate(t *testing.T) { |
| 167 | + db := newTestDB(t) |
| 168 | + ch := seedCentrehead(t, db, "ch.partial@iit.ac.in") |
| 169 | + |
| 170 | + e := newAuthRouter(db, authAs(ch.ID, ch.Email)) |
| 171 | + rec := doRequest(t, e, http.MethodPatch, "/api/centrehead/profile/edit", map[string]any{ |
| 172 | + "phone_number": "2222222222", |
| 173 | + }) |
| 174 | + |
| 175 | + assertStatus(t, rec, 200) |
| 176 | + |
| 177 | + var updated models.Centrehead |
| 178 | + db.First(&updated, ch.ID) |
| 179 | + if updated.PhoneNumber != "2222222222" { |
| 180 | + t.Fatalf("expected phone number updated, got %q", updated.PhoneNumber) |
| 181 | + } |
| 182 | + if updated.Building != ch.Building { |
| 183 | + t.Fatalf("expected untouched fields to survive a partial update, got %+v", updated) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestCentreheadProfileEdit_Unauthenticated(t *testing.T) { |
| 188 | + db := newTestDB(t) |
| 189 | + e := newAuthRouter(db, noAuth()) |
| 190 | + rec := doRequest(t, e, http.MethodPatch, "/api/centrehead/profile/edit", map[string]any{"building": "x"}) |
| 191 | + assertStatus(t, rec, 403) |
| 192 | +} |
| 193 | + |
| 194 | +// CentreheadProfileEdit's not-found branch is missing a `return` after writing |
| 195 | +// the 404, so it falls through and writes a second (500) response on top of |
| 196 | +// it. net/http.ResponseRecorder locks in the status of the first WriteHeader |
| 197 | +// call, so the recorded status is still 404 — but the body ends up as two |
| 198 | +// concatenated JSON objects, which is why this test checks the status only |
| 199 | +// and does not attempt to decode the body like its Faculty/Warden siblings do. |
| 200 | +func TestCentreheadProfileEdit_ProfileNotFound(t *testing.T) { |
| 201 | + db := newTestDB(t) |
| 202 | + e := newAuthRouter(db, authAs(1, "ghost@iit.ac.in")) |
| 203 | + rec := doRequest(t, e, http.MethodPatch, "/api/centrehead/profile/edit", map[string]any{"building": "x"}) |
| 204 | + assertStatus(t, rec, 404) |
| 205 | +} |
| 206 | + |
| 207 | +func TestCentreheadProfileEdit_InvalidBody(t *testing.T) { |
| 208 | + db := newTestDB(t) |
| 209 | + ch := seedCentrehead(t, db, "ch.badbody@iit.ac.in") |
| 210 | + e := newAuthRouter(db, authAs(ch.ID, ch.Email)) |
| 211 | + rec := doRequest(t, e, http.MethodPatch, "/api/centrehead/profile/edit", []string{"bad"}) |
| 212 | + assertStatus(t, rec, 400) |
| 213 | +} |
0 commit comments