From 4b8aca6d065d8052c9a316c15900a760c9d8d3cb Mon Sep 17 00:00:00 2001 From: ayush00git Date: Sat, 30 May 2026 17:45:38 +0530 Subject: [PATCH] fix: changed centre_head -> centrehead for role conventions --- app/src/components/ComplaintCard.tsx | 2 +- app/src/pages/admin/AEPostView.tsx | 4 +- app/src/pages/admin/AdminPostView.tsx | 6 +- app/src/pages/admin/JEPostView.tsx | 4 +- app/src/pages/admin/XENPostView.tsx | 4 +- app/src/pages/auth/AccountResetPass.tsx | 2 +- .../pages/auth/CentreHeadForgotPassword.tsx | 2 +- app/src/pages/auth/CentreHeadLogin.tsx | 2 +- app/src/pages/auth/CentreHeadSignup.tsx | 2 +- app/src/pages/post/CentreHeadPost.tsx | 2 +- app/src/pages/profile/Profile.tsx | 10 +- config/db.go | 4 +- handlers/admin_comment.go | 2 +- handlers/admin_post.go | 14 +-- handlers/admin_status.go | 6 +- handlers/auth.go | 4 +- handlers/centrehead_auth.go | 30 ++--- handlers/centrehead_post.go | 48 ++++---- models/centrehead_auth.go | 6 +- models/post.go | 8 +- routes/admin.go | 2 +- routes/auth.go | 10 +- routes/post.go | 8 +- test/admin_comment_test.go | 8 +- test/admin_post_test.go | 14 +-- test/auth_test.go | 10 +- test/centrehead_auth_test.go | 82 ++++++------- test/centrehead_post_test.go | 116 +++++++++--------- test/helpers_test.go | 28 ++--- 29 files changed, 220 insertions(+), 220 deletions(-) diff --git a/app/src/components/ComplaintCard.tsx b/app/src/components/ComplaintCard.tsx index efa6aac..e308065 100644 --- a/app/src/components/ComplaintCard.tsx +++ b/app/src/components/ComplaintCard.tsx @@ -7,7 +7,7 @@ import { POST_PLACES } from '../constants/models'; // ── Types ──────────────────────────────────────────────────────────────────────── -export type Role = 'faculty' | 'warden' | 'centre_head'; +export type Role = 'faculty' | 'warden' | 'centrehead'; export interface CommentAuthor { id: number; diff --git a/app/src/pages/admin/AEPostView.tsx b/app/src/pages/admin/AEPostView.tsx index 596fa66..24d39c0 100644 --- a/app/src/pages/admin/AEPostView.tsx +++ b/app/src/pages/admin/AEPostView.tsx @@ -16,7 +16,7 @@ interface AEPostsResponse { success: string; faculty_posts: BasePost[] | BasePost | null; warden_posts: BasePost[] | BasePost | null; - centre_head_posts: BasePost[] | BasePost | null; + centrehead_posts: BasePost[] | BasePost | null; } function normalise(val: BasePost[] | BasePost | null | undefined): BasePost[] { @@ -184,7 +184,7 @@ export function AEPostView() { ); } - const { faculty_posts: fp, warden_posts: wp, centre_head_posts: cp } = data!; + const { faculty_posts: fp, warden_posts: wp, centrehead_posts: cp } = data!; return ( diff --git a/app/src/pages/admin/AdminPostView.tsx b/app/src/pages/admin/AdminPostView.tsx index d840c59..459a116 100644 --- a/app/src/pages/admin/AdminPostView.tsx +++ b/app/src/pages/admin/AdminPostView.tsx @@ -101,7 +101,7 @@ interface WardenPost { interface CentreHeadPost { id: number; - centre_head_id: number; + centrehead_id: number; Author: CentreHeadAuthor; type_of_post: string; title: string; @@ -136,14 +136,14 @@ const STATUS_STYLES: Record = { const ROLE_TO_STATUS_API: Record = { faculty: 'faculty_posts', warden: 'warden_posts', - centrehead: 'centre_head_posts', + centrehead: 'centrehead_posts', }; // Maps URL role param → API segment for the comment endpoint const ROLE_TO_COMMENT_API: Record = { faculty: 'faculty_posts', warden: 'wardens_posts', - centrehead: 'centreheads_posts', + centrehead: 'centrehead_posts', }; // Back-link per admin type diff --git a/app/src/pages/admin/JEPostView.tsx b/app/src/pages/admin/JEPostView.tsx index 401b6e1..61c8f51 100644 --- a/app/src/pages/admin/JEPostView.tsx +++ b/app/src/pages/admin/JEPostView.tsx @@ -16,7 +16,7 @@ interface JEPostsResponse { success: string; faculty_posts: BasePost[] | BasePost | null; warden_posts: BasePost[] | BasePost | null; - centre_head_posts: BasePost[] | BasePost | null; + centrehead_posts: BasePost[] | BasePost | null; } function normalise(val: BasePost[] | BasePost | null | undefined): BasePost[] { @@ -184,7 +184,7 @@ export function JEPostView() { ); } - const { faculty_posts: fp, warden_posts: wp, centre_head_posts: cp } = data!; + const { faculty_posts: fp, warden_posts: wp, centrehead_posts: cp } = data!; return ( diff --git a/app/src/pages/admin/XENPostView.tsx b/app/src/pages/admin/XENPostView.tsx index 62eab92..52af183 100644 --- a/app/src/pages/admin/XENPostView.tsx +++ b/app/src/pages/admin/XENPostView.tsx @@ -17,7 +17,7 @@ interface XENPostsResponse { // API may return an array, a single object, or null depending on the backend faculty_posts: BasePost[] | BasePost | null; warden_posts: BasePost[] | BasePost | null; - centre_head_posts: BasePost[] | BasePost | null; + centrehead_posts: BasePost[] | BasePost | null; } // Handles every shape the Go backend might send: @@ -192,7 +192,7 @@ export function XENPostView() { ); } - const { faculty_posts: fp, warden_posts: wp, centre_head_posts: cp } = data!; + const { faculty_posts: fp, warden_posts: wp, centrehead_posts: cp } = data!; return ( diff --git a/app/src/pages/auth/AccountResetPass.tsx b/app/src/pages/auth/AccountResetPass.tsx index c351f3a..2ea683e 100644 --- a/app/src/pages/auth/AccountResetPass.tsx +++ b/app/src/pages/auth/AccountResetPass.tsx @@ -6,7 +6,7 @@ import { MainLayout } from '../../components/layout/MainLayout'; const roleToApi: Record = { faculty: '/api/auth/faculty/reset-password', warden: '/api/auth/warden/reset-password', - centrehead: '/api/auth/centre_head/reset-password', + centrehead: '/api/auth/centrehead/reset-password', }; const roleToLoginPath: Record = { diff --git a/app/src/pages/auth/CentreHeadForgotPassword.tsx b/app/src/pages/auth/CentreHeadForgotPassword.tsx index 951bc9c..d362d54 100644 --- a/app/src/pages/auth/CentreHeadForgotPassword.tsx +++ b/app/src/pages/auth/CentreHeadForgotPassword.tsx @@ -16,7 +16,7 @@ export function CentreHeadForgotPassword() { setMessage(''); try { - const response = await fetch('/api/auth/centre_head/forget-password', { + const response = await fetch('/api/auth/centrehead/forget-password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email }), diff --git a/app/src/pages/auth/CentreHeadLogin.tsx b/app/src/pages/auth/CentreHeadLogin.tsx index fe57c57..c01e8b5 100644 --- a/app/src/pages/auth/CentreHeadLogin.tsx +++ b/app/src/pages/auth/CentreHeadLogin.tsx @@ -19,7 +19,7 @@ export function CentreHeadLogin() { setMessage(''); try { - const response = await fetch('/api/auth/centre_head/login', { + const response = await fetch('/api/auth/centrehead/login', { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/app/src/pages/auth/CentreHeadSignup.tsx b/app/src/pages/auth/CentreHeadSignup.tsx index 3465a28..447afd1 100644 --- a/app/src/pages/auth/CentreHeadSignup.tsx +++ b/app/src/pages/auth/CentreHeadSignup.tsx @@ -28,7 +28,7 @@ export function CentreHeadSignup() { setMessage(''); try { - const response = await fetch('/api/auth/centre_head/signup', { + const response = await fetch('/api/auth/centrehead/signup', { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/app/src/pages/post/CentreHeadPost.tsx b/app/src/pages/post/CentreHeadPost.tsx index ea3c25b..2869b86 100644 --- a/app/src/pages/post/CentreHeadPost.tsx +++ b/app/src/pages/post/CentreHeadPost.tsx @@ -26,7 +26,7 @@ export function CentreHeadPost() { setMessage(''); try { - const response = await fetch('/api/post/centre_head', { + const response = await fetch('/api/post/centrehead', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), diff --git a/app/src/pages/profile/Profile.tsx b/app/src/pages/profile/Profile.tsx index 8159cd8..380fc78 100644 --- a/app/src/pages/profile/Profile.tsx +++ b/app/src/pages/profile/Profile.tsx @@ -48,7 +48,7 @@ export function Profile() { let endpoint = ''; if ('department' in profile) endpoint = '/api/post/faculty'; else if ('hostel' in profile) endpoint = '/api/post/warden'; - else if ('building' in profile) endpoint = '/api/post/centre_head'; + else if ('building' in profile) endpoint = '/api/post/centrehead'; else return; setPostsLoading(true); @@ -99,14 +99,14 @@ export function Profile() { let roleLabel = 'User'; let roleBadgeCls = 'bg-gray-100 text-gray-800 border-gray-200'; let registerRoute = '/'; - let role: Role = 'centre_head'; + let role: Role = 'centrehead'; if (isFaculty) { roleLabel = 'Faculty Member'; roleBadgeCls = 'bg-emerald-50 text-emerald-700 border-emerald-200'; registerRoute = '/faculty/post'; role = 'faculty'; } else if (isWarden) { roleLabel = 'Hostel Warden'; roleBadgeCls = 'bg-indigo-50 text-indigo-700 border-indigo-200'; registerRoute = '/warden/post'; role = 'warden'; } - else if (isCentreHead) { roleLabel = 'Centre Head'; roleBadgeCls = 'bg-amber-50 text-amber-700 border-amber-200'; registerRoute = '/centre-head/post'; role = 'centre_head'; } + else if (isCentreHead) { roleLabel = 'Centre Head'; roleBadgeCls = 'bg-amber-50 text-amber-700 border-amber-200'; registerRoute = '/centre-head/post'; role = 'centrehead'; } // ── API base paths ── - const editBase = isFaculty ? '/api/post/faculty/edit' : isWarden ? '/api/post/warden/edit' : '/api/post/centre_head/edit'; - const deleteBase = isFaculty ? '/api/post/faculty/delete' : isWarden ? '/api/post/warden/delete' : '/api/post/centre_head/delete'; + const editBase = isFaculty ? '/api/post/faculty/edit' : isWarden ? '/api/post/warden/edit' : '/api/post/centrehead/edit'; + const deleteBase = isFaculty ? '/api/post/faculty/delete' : isWarden ? '/api/post/warden/delete' : '/api/post/centrehead/delete'; // ── Handlers ── const handleLogout = async () => { diff --git a/config/db.go b/config/db.go index 2608198..823d356 100644 --- a/config/db.go +++ b/config/db.go @@ -32,10 +32,10 @@ func ConnectDB() { &models.Admin{}, &models.Faculty{}, &models.Warden{}, - &models.CentreHead{}, + &models.Centrehead{}, &models.FacultyPost{}, &models.WardenPost{}, - &models.CentreHeadPost{}, + &models.CentreheadPost{}, &models.Comment{}, ) diff --git a/handlers/admin_comment.go b/handlers/admin_comment.go index e0c6c69..6090d91 100644 --- a/handlers/admin_comment.go +++ b/handlers/admin_comment.go @@ -56,7 +56,7 @@ func (h *AdminHandler) AdminPostComment (c *gin.Context) { tableMap := map[string]interface{} { "faculty_posts": &models.FacultyPost{}, "warden_posts": &models.WardenPost{}, - "centre_head_posts": &models.CentreHeadPost{}, + "centrehead_posts": &models.CentreheadPost{}, } postType := c.Param("type") diff --git a/handlers/admin_post.go b/handlers/admin_post.go index 3d0f266..a6cd72f 100644 --- a/handlers/admin_post.go +++ b/handlers/admin_post.go @@ -43,7 +43,7 @@ func (h *AdminHandler) GetXENPosts (c *gin.Context) { var facultyPosts []models.FacultyPost var wardenPosts []models.WardenPost - var centreheadPosts []models.CentreHeadPost + var centreheadPosts []models.CentreheadPost // fetch faculty posts // this api only returns the fields that are required at the @@ -79,7 +79,7 @@ func (h *AdminHandler) GetXENPosts (c *gin.Context) { "success": "posts fetched successfully", "faculty_posts": facultyPosts, "warden_posts": wardenPosts, - "centre_head_posts": centreheadPosts, + "centrehead_posts": centreheadPosts, }) } @@ -118,7 +118,7 @@ func (h* AdminHandler) GetAEPosts (c *gin.Context) { var facultyPosts []models.FacultyPost var wardenPosts []models.WardenPost - var centreheadPosts []models.CentreHeadPost + var centreheadPosts []models.CentreheadPost // fetch faculty posts result = h.DB.Select("id, title, type_of_post, status, assigned_je_id"). @@ -154,7 +154,7 @@ func (h* AdminHandler) GetAEPosts (c *gin.Context) { "success": "posts fetched successfully", "faculty_posts": facultyPosts, "warden_posts": wardenPosts, - "centre_head_posts": centreheadPosts, + "centrehead_posts": centreheadPosts, }) } @@ -193,7 +193,7 @@ func (h* AdminHandler) GetJEPosts (c *gin.Context) { var facultyPosts []models.FacultyPost var wardenPosts []models.WardenPost - var centreheadPosts []models.CentreHeadPost + var centreheadPosts []models.CentreheadPost // fetch faculty posts result = h.DB.Select("id, title, type_of_post, status, assigned_je_id"). @@ -229,7 +229,7 @@ func (h* AdminHandler) GetJEPosts (c *gin.Context) { "success": "posts fetched successfully", "faculty_posts": facultyPosts, "warden_posts": wardenPosts, - "centre_head_posts": centreheadPosts, + "centrehead_posts": centreheadPosts, }) } @@ -297,7 +297,7 @@ func (h *AdminHandler) AdminGetPost (c *gin.Context) { } reqPost = post case "centrehead": - var post models.CentreHeadPost + var post models.CentreheadPost result := h.DB.Preload("Author", func (db *gorm.DB) (*gorm.DB) { return db.Select("id, email, building, phone_number") }). diff --git a/handlers/admin_status.go b/handlers/admin_status.go index 361e6bc..1f3538e 100644 --- a/handlers/admin_status.go +++ b/handlers/admin_status.go @@ -250,8 +250,8 @@ func (h *AdminHandler) AdminWardenPostStatus (c *gin.Context) { c.JSON(200, gin.H{"success": "status updated"}) } -// AdminCentreHeadPostStatus sets the stage of the centre_head posts -func (h *AdminHandler) AdminCentreHeadPostStatus (c *gin.Context) { +// AdminCentreheadPostStatus sets the stage of the centrehead posts +func (h *AdminHandler) AdminCentreheadPostStatus(c *gin.Context) { adminEmail, exists := c.Get(middleware.EmailKey) if !exists { c.JSON(401, gin.H{"error": "permission denied"}) @@ -277,7 +277,7 @@ func (h *AdminHandler) AdminCentreHeadPostStatus (c *gin.Context) { } // see if this post exists - var post models.CentreHeadPost + var post models.CentreheadPost result = h.DB.Where("id = ?", uint(postID)).Take(&post) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/handlers/auth.go b/handlers/auth.go index 5952fd4..de01b49 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -43,7 +43,7 @@ func (h *AuthHandler) VerifyAccount (c *gin.Context) { case "warden": h.DB.Model(&models.Warden{}).Where("email = ?", claims.Email).Update("is_verified", true) case "centrehead": - h.DB.Model(&models.CentreHead{}).Where("email = ?", claims.Email).Update("is_verified", true) + h.DB.Model(&models.Centrehead{}).Where("email = ?", claims.Email).Update("is_verified", true) default: c.JSON(400, gin.H{"error": "role not defined"}) return @@ -81,7 +81,7 @@ func (h *AuthHandler) UserProfile (c *gin.Context) { } userProfile = profile case "centrehead": - var profile models.CentreHead + var profile models.Centrehead result := h.DB.Where("email = ?", email).Take(&profile) if result.Error != nil { c.JSON(500, gin.H{"error": "failed to fetch user profile"}) diff --git a/handlers/centrehead_auth.go b/handlers/centrehead_auth.go index 671a827..e627c28 100644 --- a/handlers/centrehead_auth.go +++ b/handlers/centrehead_auth.go @@ -14,10 +14,10 @@ import ( ) -// CentreHeadSignup registers the head of adminstrations. +// CentreheadSignup registers the head of adminstrations. // On success, sends a verification email with a JWT token link. -func (h *AuthHandler) CentreHeadSignup(c *gin.Context) { - var inputs models.CentreHeadSignup +func (h *AuthHandler) CentreheadSignup(c *gin.Context) { + var inputs models.CentreheadSignup if err := c.ShouldBindJSON(&inputs); err != nil { c.JSON(400, gin.H{"error": "invalid request body"}) @@ -32,7 +32,7 @@ func (h *AuthHandler) CentreHeadSignup(c *gin.Context) { inputs.Password = string(hashedPass) - centrehead := models.CentreHead{ + centrehead := models.Centrehead{ Email: inputs.Email, Password: inputs.Password, Building: inputs.Building, @@ -41,7 +41,7 @@ func (h *AuthHandler) CentreHeadSignup(c *gin.Context) { CreatedAt: time.Now(), } - var existingUser models.CentreHead + var existingUser models.Centrehead result := h.DB.Where("email = ?", centrehead.Email).Take(&existingUser) if result.Error == nil { if !existingUser.IsVerified { @@ -71,17 +71,17 @@ func (h *AuthHandler) CentreHeadSignup(c *gin.Context) { } -// CentreHeadLogin authenticates the head of administrations using email and password. +// CentreheadLogin authenticates the head of administrations using email and password. // On success, signs a JWT and stores it in an httpOnly cookie. -func (h *AuthHandler) CentreHeadLogin(c *gin.Context) { - var inputs models.CentreHeadLogin +func (h *AuthHandler) CentreheadLogin(c *gin.Context) { + var inputs models.CentreheadLogin if err := c.ShouldBindJSON(&inputs); err != nil { c.JSON(400, gin.H{"error": "invalid request body"}) return } - var head models.CentreHead + var head models.Centrehead result := h.DB.Where("email = ?", inputs.Email).Take(&head) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { @@ -123,14 +123,14 @@ func (h *AuthHandler) CentreHeadLogin(c *gin.Context) { } -// CentreHeadForgetPassword sends an password reset email to the user -func (h* AuthHandler) CentreHeadForgetPassword(c *gin.Context) { +// CentreheadForgetPassword sends an password reset email to the user +func (h* AuthHandler) CentreheadForgetPassword(c *gin.Context) { var input ForgetPassword if err := c.ShouldBindJSON(&input); err != nil { c.JSON(400, gin.H{"error": "invalid request body"}) return } - var head models.CentreHead + var head models.Centrehead result := h.DB.Where("email = ?", input.Email).Take(&head) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { @@ -155,8 +155,8 @@ func (h* AuthHandler) CentreHeadForgetPassword(c *gin.Context) { } -// CentreHeadResetPassword resets the password of the user -func (h *AuthHandler) CentreHeadResetPassword(c *gin.Context) { +// CentreheadResetPassword resets the password of the user +func (h *AuthHandler) CentreheadResetPassword(c *gin.Context) { // get the user from query parameters userToken := c.Query("user") @@ -166,7 +166,7 @@ func (h *AuthHandler) CentreHeadResetPassword(c *gin.Context) { return } - var head models.CentreHead + var head models.Centrehead result := h.DB.Where("email = ?", claims.Email).Take(&head) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/handlers/centrehead_post.go b/handlers/centrehead_post.go index 2d30794..d8a66b3 100644 --- a/handlers/centrehead_post.go +++ b/handlers/centrehead_post.go @@ -11,24 +11,24 @@ import ( "gorm.io/gorm" ) -// CentreHeadPostEditType -type CentreHeadPostEditType struct { +// CentreheadPostEditType +type CentreheadPostEditType struct { Title string `json:"title"` Description string `json:"description"` UpdatedAt time.Time `json:"updated_at"` } -// CentreHeadPostType -type CentreHeadPostType struct { +// CentreheadPostType +type CentreheadPostType struct { Title string `json:"title"` Description string `json:"description"` TypeOfPost string `json:"type_of_post"` } -// CentreHeadPost registers the post of centre-head members. +// CentreheadPost registers the post of centre-head members. // forwards the post to the associated XEN. -func (h *PostHandler) CentreHeadPost (c *gin.Context) { - var inputs CentreHeadPostType +func (h *PostHandler) CentreheadPost (c *gin.Context) { + var inputs CentreheadPostType if err := c.ShouldBindJSON(&inputs); err != nil { c.JSON(400, gin.H{"error": "invalid request body"}) @@ -44,7 +44,7 @@ func (h *PostHandler) CentreHeadPost (c *gin.Context) { email, _ := c.Get(middleware.EmailKey) // read db for this email exists or not - var head models.CentreHead + var head models.Centrehead result := h.DB.Where("email = ?", email).Take(&head) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { @@ -55,8 +55,8 @@ func (h *PostHandler) CentreHeadPost (c *gin.Context) { return } - post := models.CentreHeadPost{ - CentreHeadID: centreheadId.(uint), + post := models.CentreheadPost{ + CentreheadID: centreheadId.(uint), TypeOfPost: models.PostType(inputs.TypeOfPost), Title: inputs.Title, Description: inputs.Description, @@ -74,9 +74,9 @@ func (h *PostHandler) CentreHeadPost (c *gin.Context) { } -// CentreHeadPostEdit let's the author of the post edit it. +// CentreheadPostEdit let's the author of the post edit it. // Match is the author trying to edit. -func (h *PostHandler) CentreHeadPostEdit (c *gin.Context) { +func (h *PostHandler) CentreheadPostEdit (c *gin.Context) { // get the id of the user from gin context userID, exists := c.Get(middleware.UserIDKey) if !exists { @@ -84,7 +84,7 @@ func (h *PostHandler) CentreHeadPostEdit (c *gin.Context) { return } - var post models.CentreHeadPost + var post models.CentreheadPost postID := c.Param("post_id") result := h.DB.Where("id = ?", postID).Take(&post) if result.Error != nil { @@ -97,12 +97,12 @@ func (h *PostHandler) CentreHeadPostEdit (c *gin.Context) { } // verifying is it the author trying to edit - if post.CentreHeadID != userID.(uint) { + if post.CentreheadID != userID.(uint) { c.JSON(403, gin.H{"error": "you are not authorized for this action"}) return } - var inputs CentreHeadPostEditType + var inputs CentreheadPostEditType inputs.UpdatedAt = time.Now() if err := c.ShouldBindJSON(&inputs); err != nil { c.JSON(400, gin.H{"error": "invalid request body"}) @@ -119,9 +119,9 @@ func (h *PostHandler) CentreHeadPostEdit (c *gin.Context) { } -// CentreHeadPostDelete lets the author delete his post. +// CentreheadPostDelete lets the author delete his post. // Matches is the author trying to delete. -func (h *PostHandler) CentreHeadPostDelete (c *gin.Context) { +func (h *PostHandler) CentreheadPostDelete (c *gin.Context) { // get userID from gin context userID, exists := c.Get(middleware.UserIDKey); if !exists { @@ -131,7 +131,7 @@ func (h *PostHandler) CentreHeadPostDelete (c *gin.Context) { // get postID from parameters and fetch the post postID := c.Param("post_id") - var post models.CentreHeadPost + var post models.CentreheadPost result := h.DB.Where("id = ?", postID).Take(&post) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { @@ -142,7 +142,7 @@ func (h *PostHandler) CentreHeadPostDelete (c *gin.Context) { return } - if post.CentreHeadID != userID.(uint) { + if post.CentreheadID != userID.(uint) { c.JSON(403, gin.H{"error": "you are not authorized for this action"}) return } @@ -156,29 +156,29 @@ func (h *PostHandler) CentreHeadPostDelete (c *gin.Context) { } -// GetCentreHeadPosts fetch the posts of the centre head member along with their status and comments -func (h *PostHandler) GetCentreHeadPosts (c *gin.Context) { +// GetCentreheadPosts fetch the posts of the centre head member along with their status and comments +func (h *PostHandler) GetCentreheadPosts (c *gin.Context) { email, exists := c.Get(middleware.EmailKey) if !exists { c.JSON(401, gin.H{"error": "unauthenticated user"}) return } - var head models.CentreHead + var head models.Centrehead result := h.DB.Where("email = ?", email).Take(&head) if result.Error != nil { c.JSON(401, gin.H{"error": "user not found"}) return } - var posts []models.CentreHeadPost + var posts []models.CentreheadPost result = h.DB. Preload("Comments", func(db *gorm.DB) (*gorm.DB) { return db.Preload("Author", func (d *gorm.DB) (*gorm.DB) { return d.Select("id, email, position") }) }). - Where("centre_head_id = ?", head.ID). + Where("centrehead_id = ?", head.ID). Find(&posts) if result.Error != nil { c.JSON(500, gin.H{"error": "failed to fetch posts at the moment"}) diff --git a/models/centrehead_auth.go b/models/centrehead_auth.go index 61598c9..4625bcc 100644 --- a/models/centrehead_auth.go +++ b/models/centrehead_auth.go @@ -27,7 +27,7 @@ const ( ShoppingComplex BuildingName = "Shopping Complex (near residential area)" ) -type CentreHead struct { +type Centrehead struct { ID uint `gorm:"primaryKey;autoIncrement" json:"id"` Email string `gorm:"uniqueIndex;not null" json:"email"` Password string `gorm:"not null" json:"password"` @@ -37,14 +37,14 @@ type CentreHead struct { CreatedAt time.Time `json:"created_at"` } -type CentreHeadSignup struct { +type CentreheadSignup struct { Email string `json:"email" binding:"required"` Password string `json:"password" binding:"required"` Building BuildingName `json:"building" binding:"required"` PhoneNumber string `json:"phone_number" binding:"required"` } -type CentreHeadLogin struct { +type CentreheadLogin struct { Email string `json:"email" binding:"required"` Password string `json:"password" binding:"required"` } diff --git a/models/post.go b/models/post.go index 2848f7c..2f4718c 100644 --- a/models/post.go +++ b/models/post.go @@ -8,7 +8,7 @@ type PostSource string const ( SourceFaculty PostSource = "Faculty" SourceWarden PostSource = "Warden" - SourceCentreHead PostSource = "CentreHead" + SourceCentrehead PostSource = "Centrehead" ) type PostPlace string @@ -74,10 +74,10 @@ type WardenPost struct { Comments []Comment `gorm:"polymorphic:Commentable;" json:"comments"` } -type CentreHeadPost struct { +type CentreheadPost struct { ID uint `gorm:"primaryKey;autoIncrement" json:"id"` - CentreHeadID uint `gorm:"not null" json:"centre_head_id"` - Author CentreHead `gorm:"foreignKey:CentreHeadID"` + CentreheadID uint `gorm:"not null" json:"centrehead_id"` + Author Centrehead `gorm:"foreignKey:CentreheadID"` TypeOfPost PostType `gorm:"type:varchar(20);not null" json:"type_of_post" binding:"required"` Title string `gorm:"not null" json:"title" binding:"required"` Description string `gorm:"type:text;not null" json:"description" binding:"required"` diff --git a/routes/admin.go b/routes/admin.go index 9d03e10..13678f1 100644 --- a/routes/admin.go +++ b/routes/admin.go @@ -17,7 +17,7 @@ func AdminRoutes (e *gin.Engine, h *handlers.AdminHandler) { { stage.PATCH("/faculty_posts/status/:post_id", middleware.IsAuthenticated(), h.AdminFacultyPostStatus) stage.PATCH("/warden_posts/status/:post_id", middleware.IsAuthenticated(), h.AdminWardenPostStatus) - stage.PATCH("/centre_head_posts/status/:post_id", middleware.IsAuthenticated(), h.AdminCentreHeadPostStatus) + stage.PATCH("/centrehead_posts/status/:post_id", middleware.IsAuthenticated(), h.AdminCentreheadPostStatus) } // get the posts according to the status diff --git a/routes/auth.go b/routes/auth.go index 7e6e90e..de9f361 100644 --- a/routes/auth.go +++ b/routes/auth.go @@ -21,12 +21,12 @@ func AuthRoute (e *gin.Engine, h *handlers.AuthHandler) { warden.POST("/forget-password", h.WardenForgetPassword) warden.PATCH("/reset-password", h.WardenResetPassword) } - centrehead := e.Group("/api/auth/centre_head") + centrehead := e.Group("/api/auth/centrehead") { - centrehead.POST("/signup", h.CentreHeadSignup) - centrehead.POST("/login", h.CentreHeadLogin) - centrehead.POST("/forget-password", h.CentreHeadForgetPassword) - centrehead.PATCH("/reset-password", h.CentreHeadResetPassword) + centrehead.POST("/signup", h.CentreheadSignup) + centrehead.POST("/login", h.CentreheadLogin) + centrehead.POST("/forget-password", h.CentreheadForgetPassword) + centrehead.PATCH("/reset-password", h.CentreheadResetPassword) } e.POST("/api/auth/logout", h.Logout) diff --git a/routes/post.go b/routes/post.go index 376314b..a619df8 100644 --- a/routes/post.go +++ b/routes/post.go @@ -10,17 +10,17 @@ import ( func PostRoute(e *gin.Engine, h *handlers.PostHandler) { e.POST("/api/post/faculty", middleware.IsAuthenticated(), h.FacultyPost) e.POST("/api/post/warden", middleware.IsAuthenticated(), h.WardenPost) - e.POST("/api/post/centre_head", middleware.IsAuthenticated(), h.CentreHeadPost) + e.POST("/api/post/centrehead", middleware.IsAuthenticated(), h.CentreheadPost) e.PATCH("/api/post/faculty/edit/:post_id", middleware.IsAuthenticated(), h.FacultyPostEdit) e.PATCH("/api/post/warden/edit/:post_id", middleware.IsAuthenticated(), h.WardenPostEdit) - e.PATCH("/api/post/centre_head/edit/:post_id", middleware.IsAuthenticated(), h.CentreHeadPostEdit) + e.PATCH("/api/post/centrehead/edit/:post_id", middleware.IsAuthenticated(), h.CentreheadPostEdit) e.DELETE("/api/post/faculty/delete/:post_id", middleware.IsAuthenticated(), h.FacultyPostDelete) e.DELETE("/api/post/warden/delete/:post_id", middleware.IsAuthenticated(), h.WardenPostDelete) - e.DELETE("/api/post/centre_head/delete/:post_id", middleware.IsAuthenticated(), h.CentreHeadPostDelete) + e.DELETE("/api/post/centrehead/delete/:post_id", middleware.IsAuthenticated(), h.CentreheadPostDelete) e.GET("/api/post/faculty", middleware.IsAuthenticated(), h.GetFacultyPosts) e.GET("/api/post/warden", middleware.IsAuthenticated(), h.GetWardenPosts) - e.GET("/api/post/centre_head", middleware.IsAuthenticated(), h.GetCentreHeadPosts) + e.GET("/api/post/centrehead", middleware.IsAuthenticated(), h.GetCentreheadPosts) } diff --git a/test/admin_comment_test.go b/test/admin_comment_test.go index fb676cc..ec7b077 100644 --- a/test/admin_comment_test.go +++ b/test/admin_comment_test.go @@ -60,15 +60,15 @@ func TestAdminPostComment_Warden(t *testing.T) { assertStatus(t, rec, 201) } -func TestAdminPostComment_CentreHead(t *testing.T) { +func TestAdminPostComment_Centrehead(t *testing.T) { db := newTestDB(t) admin := seedAdmin(t, db, "admin.commentc@iit.ac.in", models.TypeXENCivil) - ch := seedCentreHead(t, db, "ch.comment@iit.ac.in") - post := models.CentreHeadPost{CentreHeadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} + ch := seedCentrehead(t, db, "ch.comment@iit.ac.in") + post := models.CentreheadPost{CentreheadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} db.Create(&post) e := newAdminCommentRouter(db, authAs(admin.ID, admin.Email)) - rec := doRequest(t, e, http.MethodPost, "/api/admin/comment/centre_head_posts/1", handlers.CommentType{Content: "noted"}) + rec := doRequest(t, e, http.MethodPost, "/api/admin/comment/centrehead_posts/1", handlers.CommentType{Content: "noted"}) assertStatus(t, rec, 201) } diff --git a/test/admin_post_test.go b/test/admin_post_test.go index 1a33a57..27fdcc6 100644 --- a/test/admin_post_test.go +++ b/test/admin_post_test.go @@ -16,7 +16,7 @@ func TestGetXENPosts_Success(t *testing.T) { // In-scope: Civil + a status the XEN view cares about. db.Create(&models.FacultyPost{FacultyID: 1, Place: models.PlaceDepartmental, TypeOfPost: models.TypeCivil, Title: "fac", Description: "d", Status: models.StatusPendingXEN}) db.Create(&models.WardenPost{WardenID: 1, RoomNumber: "A-1", TypeOfPost: models.TypeCivil, Title: "war", Description: "d", Status: models.StatusResolved}) - db.Create(&models.CentreHeadPost{CentreHeadID: 1, TypeOfPost: models.TypeCivil, Title: "ch", Description: "d", Status: models.StatusRejected}) + db.Create(&models.CentreheadPost{CentreheadID: 1, TypeOfPost: models.TypeCivil, Title: "ch", Description: "d", Status: models.StatusRejected}) // Out-of-scope: Electrical type should be filtered out for a Civil XEN. db.Create(&models.FacultyPost{FacultyID: 1, Place: models.PlaceDepartmental, TypeOfPost: models.TypeElectrical, Title: "elec", Description: "d", Status: models.StatusPendingXEN}) @@ -31,7 +31,7 @@ func TestGetXENPosts_Success(t *testing.T) { if wp := out["warden_posts"].([]any); len(wp) != 1 { t.Fatalf("expected 1 civil warden post, got %d", len(wp)) } - if cp := out["centre_head_posts"].([]any); len(cp) != 1 { + if cp := out["centrehead_posts"].([]any); len(cp) != 1 { t.Fatalf("expected 1 civil centre head post, got %d", len(cp)) } } @@ -106,7 +106,7 @@ func TestGetJEPosts_Success(t *testing.T) { admin := seedAdmin(t, db, "je.civil2@iit.ac.in", models.TypeJECivil) db.Create(&models.FacultyPost{FacultyID: 1, Place: models.PlaceDepartmental, TypeOfPost: models.TypeCivil, Title: "fac", Description: "d", Status: models.StatusPendingJE}) - db.Create(&models.CentreHeadPost{CentreHeadID: 1, TypeOfPost: models.TypeCivil, Title: "ch", Description: "d", Status: models.StatusResolvedJE}) + db.Create(&models.CentreheadPost{CentreheadID: 1, TypeOfPost: models.TypeCivil, Title: "ch", Description: "d", Status: models.StatusResolvedJE}) // Out-of-scope status for the JE view. db.Create(&models.FacultyPost{FacultyID: 1, Place: models.PlaceDepartmental, TypeOfPost: models.TypeCivil, Title: "x", Description: "d", Status: models.StatusPendingXEN}) @@ -118,7 +118,7 @@ func TestGetJEPosts_Success(t *testing.T) { if fp := out["faculty_posts"].([]any); len(fp) != 1 { t.Fatalf("expected 1 in-scope faculty post, got %d", len(fp)) } - if cp := out["centre_head_posts"].([]any); len(cp) != 1 { + if cp := out["centrehead_posts"].([]any); len(cp) != 1 { t.Fatalf("expected 1 in-scope centre head post, got %d", len(cp)) } } @@ -169,11 +169,11 @@ func TestAdminGetPost_Warden(t *testing.T) { assertStatus(t, rec, 200) } -func TestAdminGetPost_CentreHead(t *testing.T) { +func TestAdminGetPost_Centrehead(t *testing.T) { db := newTestDB(t) admin := seedAdmin(t, db, "admin.getc@iit.ac.in", models.TypeXENCivil) - ch := seedCentreHead(t, db, "ch.adminget@iit.ac.in") - post := models.CentreHeadPost{CentreHeadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} + ch := seedCentrehead(t, db, "ch.adminget@iit.ac.in") + post := models.CentreheadPost{CentreheadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} db.Create(&post) e := newAdminRouter(db, authAs(admin.ID, admin.Email)) diff --git a/test/auth_test.go b/test/auth_test.go index 89217bc..5c4962d 100644 --- a/test/auth_test.go +++ b/test/auth_test.go @@ -58,9 +58,9 @@ func TestVerifyAccount_Warden(t *testing.T) { } } -func TestVerifyAccount_CentreHead(t *testing.T) { +func TestVerifyAccount_Centrehead(t *testing.T) { db := newTestDB(t) - ch := models.CentreHead{Email: "ch.verify@iit.ac.in", Password: testPasswordHash, Building: models.LHC, PhoneNumber: "7777777777", IsVerified: false} + ch := models.Centrehead{Email: "ch.verify@iit.ac.in", Password: testPasswordHash, Building: models.LHC, PhoneNumber: "7777777777", IsVerified: false} db.Create(&ch) token := genToken(t, ch.ID, ch.Email, "centrehead") @@ -68,7 +68,7 @@ func TestVerifyAccount_CentreHead(t *testing.T) { rec := doRequest(t, e, http.MethodGet, "/api/auth/verify?token="+token, nil) assertStatus(t, rec, 200) - var updated models.CentreHead + var updated models.Centrehead db.First(&updated, ch.ID) if !updated.IsVerified { t.Fatalf("expected centre head to be marked verified") @@ -115,9 +115,9 @@ func TestUserProfile_Warden(t *testing.T) { assertStatus(t, rec, 200) } -func TestUserProfile_CentreHead(t *testing.T) { +func TestUserProfile_Centrehead(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.profile@iit.ac.in") + ch := seedCentrehead(t, db, "ch.profile@iit.ac.in") e := newAuthRouter(db, authAsRole(ch.ID, ch.Email, "centrehead")) rec := doRequest(t, e, http.MethodGet, "/api/profile", nil) diff --git a/test/centrehead_auth_test.go b/test/centrehead_auth_test.go index e9808bd..97f431d 100644 --- a/test/centrehead_auth_test.go +++ b/test/centrehead_auth_test.go @@ -9,18 +9,18 @@ import ( "golang.org/x/crypto/bcrypt" ) -// --- CentreHeadSignup ------------------------------------------------------- +// --- CentreheadSignup ------------------------------------------------------- -func TestCentreHeadSignup_InvalidBody(t *testing.T) { +func TestCentreheadSignup_InvalidBody(t *testing.T) { db := newTestDB(t) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/signup", []string{"bad"}) + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/signup", []string{"bad"}) assertStatus(t, rec, 400) } -func TestCentreHeadSignup_AlreadyRegistered(t *testing.T) { +func TestCentreheadSignup_AlreadyRegistered(t *testing.T) { db := newTestDB(t) - seedCentreHead(t, db, "ch.dup@iit.ac.in") + seedCentrehead(t, db, "ch.dup@iit.ac.in") e := newAuthRouter(db, noAuth()) body := map[string]any{ @@ -29,18 +29,18 @@ func TestCentreHeadSignup_AlreadyRegistered(t *testing.T) { "building": string(models.LHC), "phone_number": "7777777777", } - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/signup", body) + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/signup", body) assertStatus(t, rec, 409) } -// --- CentreHeadLogin -------------------------------------------------------- +// --- CentreheadLogin -------------------------------------------------------- -func TestCentreHeadLogin_Success(t *testing.T) { +func TestCentreheadLogin_Success(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.login@iit.ac.in") + ch := seedCentrehead(t, db, "ch.login@iit.ac.in") e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/login", map[string]any{ + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/login", map[string]any{ "email": ch.Email, "password": testPassword, }) @@ -52,117 +52,117 @@ func TestCentreHeadLogin_Success(t *testing.T) { } } -func TestCentreHeadLogin_InvalidBody(t *testing.T) { +func TestCentreheadLogin_InvalidBody(t *testing.T) { db := newTestDB(t) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/login", []string{"bad"}) + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/login", []string{"bad"}) assertStatus(t, rec, 400) } -func TestCentreHeadLogin_UserNotFound(t *testing.T) { +func TestCentreheadLogin_UserNotFound(t *testing.T) { db := newTestDB(t) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/login", map[string]any{ + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/login", map[string]any{ "email": "ghost@iit.ac.in", "password": testPassword, }) assertStatus(t, rec, 404) } -func TestCentreHeadLogin_Unverified(t *testing.T) { +func TestCentreheadLogin_Unverified(t *testing.T) { db := newTestDB(t) - ch := models.CentreHead{Email: "ch.unv@iit.ac.in", Password: testPasswordHash, Building: models.LHC, PhoneNumber: "7777777777", IsVerified: false} + ch := models.Centrehead{Email: "ch.unv@iit.ac.in", Password: testPasswordHash, Building: models.LHC, PhoneNumber: "7777777777", IsVerified: false} db.Create(&ch) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/login", map[string]any{ + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/login", map[string]any{ "email": ch.Email, "password": testPassword, }) assertStatus(t, rec, 403) } -// CentreHeadLogin returns 403 (not 401) on a bad password — locking in the +// CentreheadLogin returns 403 (not 401) on a bad password — locking in the // handler's current behaviour. -func TestCentreHeadLogin_WrongPassword(t *testing.T) { +func TestCentreheadLogin_WrongPassword(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.wrongpw@iit.ac.in") + ch := seedCentrehead(t, db, "ch.wrongpw@iit.ac.in") e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/login", map[string]any{ + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/login", map[string]any{ "email": ch.Email, "password": "nope", }) assertStatus(t, rec, 401) } -// --- CentreHeadForgetPassword ----------------------------------------------- +// --- CentreheadForgetPassword ----------------------------------------------- -func TestCentreHeadForgetPassword_InvalidBody(t *testing.T) { +func TestCentreheadForgetPassword_InvalidBody(t *testing.T) { db := newTestDB(t) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/forget-password", []string{"bad"}) + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/forget-password", []string{"bad"}) assertStatus(t, rec, 400) } -func TestCentreHeadForgetPassword_UserNotFound(t *testing.T) { +func TestCentreheadForgetPassword_UserNotFound(t *testing.T) { db := newTestDB(t) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/forget-password", map[string]any{"email": "ghost@iit.ac.in"}) + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/forget-password", map[string]any{"email": "ghost@iit.ac.in"}) assertStatus(t, rec, 404) } -func TestCentreHeadForgetPassword_Unverified(t *testing.T) { +func TestCentreheadForgetPassword_Unverified(t *testing.T) { db := newTestDB(t) - ch := models.CentreHead{Email: "ch.fpunv@iit.ac.in", Password: testPasswordHash, Building: models.LHC, PhoneNumber: "7777777777", IsVerified: false} + ch := models.Centrehead{Email: "ch.fpunv@iit.ac.in", Password: testPasswordHash, Building: models.LHC, PhoneNumber: "7777777777", IsVerified: false} db.Create(&ch) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPost, "/api/auth/centre_head/forget-password", map[string]any{"email": ch.Email}) + rec := doRequest(t, e, http.MethodPost, "/api/auth/centrehead/forget-password", map[string]any{"email": ch.Email}) assertStatus(t, rec, 403) } -// --- CentreHeadResetPassword ------------------------------------------------ +// --- CentreheadResetPassword ------------------------------------------------ -func TestCentreHeadResetPassword_Success(t *testing.T) { +func TestCentreheadResetPassword_Success(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.reset@iit.ac.in") + ch := seedCentrehead(t, db, "ch.reset@iit.ac.in") token := genToken(t, ch.ID, ch.Email, "centrehead") e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPatch, "/api/auth/centre_head/reset-password?user="+token, map[string]any{ + rec := doRequest(t, e, http.MethodPatch, "/api/auth/centrehead/reset-password?user="+token, map[string]any{ "password": "BrandNewPass1", }) assertStatus(t, rec, 200) - var updated models.CentreHead + var updated models.Centrehead db.First(&updated, ch.ID) if err := bcrypt.CompareHashAndPassword([]byte(updated.Password), []byte("BrandNewPass1")); err != nil { t.Fatalf("password was not updated: %v", err) } } -func TestCentreHeadResetPassword_InvalidToken(t *testing.T) { +func TestCentreheadResetPassword_InvalidToken(t *testing.T) { db := newTestDB(t) e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPatch, "/api/auth/centre_head/reset-password?user=garbage", map[string]any{"password": "x"}) + rec := doRequest(t, e, http.MethodPatch, "/api/auth/centrehead/reset-password?user=garbage", map[string]any{"password": "x"}) assertStatus(t, rec, 500) } -func TestCentreHeadResetPassword_UserNotFound(t *testing.T) { +func TestCentreheadResetPassword_UserNotFound(t *testing.T) { db := newTestDB(t) token := genToken(t, 1, "ghost@iit.ac.in", "centrehead") e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPatch, "/api/auth/centre_head/reset-password?user="+token, map[string]any{"password": "x"}) + rec := doRequest(t, e, http.MethodPatch, "/api/auth/centrehead/reset-password?user="+token, map[string]any{"password": "x"}) assertStatus(t, rec, 403) } -func TestCentreHeadResetPassword_InvalidBody(t *testing.T) { +func TestCentreheadResetPassword_InvalidBody(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.resetbad@iit.ac.in") + ch := seedCentrehead(t, db, "ch.resetbad@iit.ac.in") token := genToken(t, ch.ID, ch.Email, "centrehead") e := newAuthRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPatch, "/api/auth/centre_head/reset-password?user="+token, []string{"bad"}) + rec := doRequest(t, e, http.MethodPatch, "/api/auth/centrehead/reset-password?user="+token, []string{"bad"}) assertStatus(t, rec, 400) } diff --git a/test/centrehead_post_test.go b/test/centrehead_post_test.go index 50ea37e..259ac05 100644 --- a/test/centrehead_post_test.go +++ b/test/centrehead_post_test.go @@ -7,11 +7,11 @@ import ( "github.com/ayush00git/cms-web/models" ) -// --- CentreHeadPost (create) ------------------------------------------------ +// --- CentreheadPost (create) ------------------------------------------------ -func TestCentreHeadPost_Create_Success(t *testing.T) { +func TestCentreheadPost_Create_Success(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.create@iit.ac.in") + ch := seedCentrehead(t, db, "ch.create@iit.ac.in") e := newPostRouter(db, authAs(ch.ID, ch.Email)) body := map[string]any{ @@ -19,148 +19,148 @@ func TestCentreHeadPost_Create_Success(t *testing.T) { "title": "Broken door", "description": "Main entrance door of LHC is jammed", } - rec := doRequest(t, e, http.MethodPost, "/api/post/centre_head", body) + rec := doRequest(t, e, http.MethodPost, "/api/post/centrehead", body) assertStatus(t, rec, 201) var count int64 - db.Model(&models.CentreHeadPost{}).Where("centre_head_id = ?", ch.ID).Count(&count) + db.Model(&models.CentreheadPost{}).Where("centrehead_id = ?", ch.ID).Count(&count) if count != 1 { t.Fatalf("expected 1 persisted post, got %d", count) } } -func TestCentreHeadPost_Create_Unauthenticated(t *testing.T) { +func TestCentreheadPost_Create_Unauthenticated(t *testing.T) { db := newTestDB(t) e := newPostRouter(db, noAuth()) body := map[string]any{"type_of_post": "Civil", "title": "x", "description": "y"} - rec := doRequest(t, e, http.MethodPost, "/api/post/centre_head", body) + rec := doRequest(t, e, http.MethodPost, "/api/post/centrehead", body) assertStatus(t, rec, 401) } -func TestCentreHeadPost_Create_InvalidBody(t *testing.T) { +func TestCentreheadPost_Create_InvalidBody(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.badbody@iit.ac.in") + ch := seedCentrehead(t, db, "ch.badbody@iit.ac.in") e := newPostRouter(db, authAs(ch.ID, ch.Email)) - rec := doRequest(t, e, http.MethodPost, "/api/post/centre_head", []string{"bad"}) + rec := doRequest(t, e, http.MethodPost, "/api/post/centrehead", []string{"bad"}) assertStatus(t, rec, 400) } -func TestCentreHeadPost_Create_UserNotFound(t *testing.T) { +func TestCentreheadPost_Create_UserNotFound(t *testing.T) { db := newTestDB(t) e := newPostRouter(db, authAs(999, "ghost.ch@iit.ac.in")) body := map[string]any{"type_of_post": "Civil", "title": "x", "description": "y"} - rec := doRequest(t, e, http.MethodPost, "/api/post/centre_head", body) + rec := doRequest(t, e, http.MethodPost, "/api/post/centrehead", body) assertStatus(t, rec, 401) } -// --- CentreHeadPostEdit ----------------------------------------------------- +// --- CentreheadPostEdit ----------------------------------------------------- -func TestCentreHeadPostEdit_Success(t *testing.T) { +func TestCentreheadPostEdit_Success(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.edit@iit.ac.in") - post := models.CentreHeadPost{CentreHeadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "old", Description: "old"} + ch := seedCentrehead(t, db, "ch.edit@iit.ac.in") + post := models.CentreheadPost{CentreheadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "old", Description: "old"} db.Create(&post) e := newPostRouter(db, authAs(ch.ID, ch.Email)) - rec := doRequest(t, e, http.MethodPatch, "/api/post/centre_head/edit/1", map[string]any{ + rec := doRequest(t, e, http.MethodPatch, "/api/post/centrehead/edit/1", map[string]any{ "title": "new title", "description": "new desc", }) assertStatus(t, rec, 200) - var updated models.CentreHeadPost + var updated models.CentreheadPost db.First(&updated, post.ID) if updated.Title != "new title" { t.Fatalf("expected title updated, got %q", updated.Title) } } -func TestCentreHeadPostEdit_NotFound(t *testing.T) { +func TestCentreheadPostEdit_NotFound(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.editnf@iit.ac.in") + ch := seedCentrehead(t, db, "ch.editnf@iit.ac.in") e := newPostRouter(db, authAs(ch.ID, ch.Email)) - rec := doRequest(t, e, http.MethodPatch, "/api/post/centre_head/edit/999", map[string]any{"title": "x"}) + rec := doRequest(t, e, http.MethodPatch, "/api/post/centrehead/edit/999", map[string]any{"title": "x"}) assertStatus(t, rec, 404) } -func TestCentreHeadPostEdit_WrongAuthor(t *testing.T) { +func TestCentreheadPostEdit_WrongAuthor(t *testing.T) { db := newTestDB(t) - owner := seedCentreHead(t, db, "ch.owner@iit.ac.in") - other := seedCentreHead(t, db, "ch.other@iit.ac.in") - post := models.CentreHeadPost{CentreHeadID: owner.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} + owner := seedCentrehead(t, db, "ch.owner@iit.ac.in") + other := seedCentrehead(t, db, "ch.other@iit.ac.in") + post := models.CentreheadPost{CentreheadID: owner.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} db.Create(&post) e := newPostRouter(db, authAs(other.ID, other.Email)) - rec := doRequest(t, e, http.MethodPatch, "/api/post/centre_head/edit/1", map[string]any{"title": "hijack"}) + rec := doRequest(t, e, http.MethodPatch, "/api/post/centrehead/edit/1", map[string]any{"title": "hijack"}) assertStatus(t, rec, 403) } -func TestCentreHeadPostEdit_Unauthenticated(t *testing.T) { +func TestCentreheadPostEdit_Unauthenticated(t *testing.T) { db := newTestDB(t) e := newPostRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodPatch, "/api/post/centre_head/edit/1", map[string]any{"title": "x"}) + rec := doRequest(t, e, http.MethodPatch, "/api/post/centrehead/edit/1", map[string]any{"title": "x"}) assertStatus(t, rec, 401) } -// --- CentreHeadPostDelete --------------------------------------------------- +// --- CentreheadPostDelete --------------------------------------------------- -func TestCentreHeadPostDelete_Success(t *testing.T) { +func TestCentreheadPostDelete_Success(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.del@iit.ac.in") - post := models.CentreHeadPost{CentreHeadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} + ch := seedCentrehead(t, db, "ch.del@iit.ac.in") + post := models.CentreheadPost{CentreheadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} db.Create(&post) e := newPostRouter(db, authAs(ch.ID, ch.Email)) - rec := doRequest(t, e, http.MethodDelete, "/api/post/centre_head/delete/1", nil) + rec := doRequest(t, e, http.MethodDelete, "/api/post/centrehead/delete/1", nil) assertStatus(t, rec, 200) var count int64 - db.Model(&models.CentreHeadPost{}).Count(&count) + db.Model(&models.CentreheadPost{}).Count(&count) if count != 0 { t.Fatalf("expected post deleted, %d remain", count) } } -func TestCentreHeadPostDelete_NotFound(t *testing.T) { +func TestCentreheadPostDelete_NotFound(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.delnf@iit.ac.in") + ch := seedCentrehead(t, db, "ch.delnf@iit.ac.in") e := newPostRouter(db, authAs(ch.ID, ch.Email)) - rec := doRequest(t, e, http.MethodDelete, "/api/post/centre_head/delete/77", nil) + rec := doRequest(t, e, http.MethodDelete, "/api/post/centrehead/delete/77", nil) assertStatus(t, rec, 404) } -func TestCentreHeadPostDelete_WrongAuthor(t *testing.T) { +func TestCentreheadPostDelete_WrongAuthor(t *testing.T) { db := newTestDB(t) - owner := seedCentreHead(t, db, "ch.delowner@iit.ac.in") - other := seedCentreHead(t, db, "ch.delother@iit.ac.in") - post := models.CentreHeadPost{CentreHeadID: owner.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} + owner := seedCentrehead(t, db, "ch.delowner@iit.ac.in") + other := seedCentrehead(t, db, "ch.delother@iit.ac.in") + post := models.CentreheadPost{CentreheadID: owner.ID, TypeOfPost: models.TypeCivil, Title: "t", Description: "d"} db.Create(&post) e := newPostRouter(db, authAs(other.ID, other.Email)) - rec := doRequest(t, e, http.MethodDelete, "/api/post/centre_head/delete/1", nil) + rec := doRequest(t, e, http.MethodDelete, "/api/post/centrehead/delete/1", nil) assertStatus(t, rec, 403) } -func TestCentreHeadPostDelete_Unauthenticated(t *testing.T) { +func TestCentreheadPostDelete_Unauthenticated(t *testing.T) { db := newTestDB(t) e := newPostRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodDelete, "/api/post/centre_head/delete/1", nil) + rec := doRequest(t, e, http.MethodDelete, "/api/post/centrehead/delete/1", nil) assertStatus(t, rec, 401) } -// --- GetCentreHeadPosts ----------------------------------------------------- +// --- GetCentreheadPosts ----------------------------------------------------- -func TestGetCentreHeadPosts_Success(t *testing.T) { +func TestGetCentreheadPosts_Success(t *testing.T) { db := newTestDB(t) - ch := seedCentreHead(t, db, "ch.get@iit.ac.in") - db.Create(&models.CentreHeadPost{CentreHeadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "a", Description: "d"}) - db.Create(&models.CentreHeadPost{CentreHeadID: ch.ID, TypeOfPost: models.TypeElectrical, Title: "b", Description: "d"}) + ch := seedCentrehead(t, db, "ch.get@iit.ac.in") + db.Create(&models.CentreheadPost{CentreheadID: ch.ID, TypeOfPost: models.TypeCivil, Title: "a", Description: "d"}) + db.Create(&models.CentreheadPost{CentreheadID: ch.ID, TypeOfPost: models.TypeElectrical, Title: "b", Description: "d"}) e := newPostRouter(db, authAs(ch.ID, ch.Email)) - rec := doRequest(t, e, http.MethodGet, "/api/post/centre_head", nil) + rec := doRequest(t, e, http.MethodGet, "/api/post/centrehead", nil) assertStatus(t, rec, 200) out := decodeBody(t, rec) @@ -170,15 +170,15 @@ func TestGetCentreHeadPosts_Success(t *testing.T) { } } -func TestGetCentreHeadPosts_OnlyOwn(t *testing.T) { +func TestGetCentreheadPosts_OnlyOwn(t *testing.T) { db := newTestDB(t) - mine := seedCentreHead(t, db, "ch.mine@iit.ac.in") - theirs := seedCentreHead(t, db, "ch.theirs@iit.ac.in") - db.Create(&models.CentreHeadPost{CentreHeadID: mine.ID, TypeOfPost: models.TypeCivil, Title: "mine", Description: "d"}) - db.Create(&models.CentreHeadPost{CentreHeadID: theirs.ID, TypeOfPost: models.TypeCivil, Title: "theirs", Description: "d"}) + mine := seedCentrehead(t, db, "ch.mine@iit.ac.in") + theirs := seedCentrehead(t, db, "ch.theirs@iit.ac.in") + db.Create(&models.CentreheadPost{CentreheadID: mine.ID, TypeOfPost: models.TypeCivil, Title: "mine", Description: "d"}) + db.Create(&models.CentreheadPost{CentreheadID: theirs.ID, TypeOfPost: models.TypeCivil, Title: "theirs", Description: "d"}) e := newPostRouter(db, authAs(mine.ID, mine.Email)) - rec := doRequest(t, e, http.MethodGet, "/api/post/centre_head", nil) + rec := doRequest(t, e, http.MethodGet, "/api/post/centrehead", nil) assertStatus(t, rec, 200) out := decodeBody(t, rec) @@ -187,9 +187,9 @@ func TestGetCentreHeadPosts_OnlyOwn(t *testing.T) { } } -func TestGetCentreHeadPosts_Unauthenticated(t *testing.T) { +func TestGetCentreheadPosts_Unauthenticated(t *testing.T) { db := newTestDB(t) e := newPostRouter(db, noAuth()) - rec := doRequest(t, e, http.MethodGet, "/api/post/centre_head", nil) + rec := doRequest(t, e, http.MethodGet, "/api/post/centrehead", nil) assertStatus(t, rec, 401) } diff --git a/test/helpers_test.go b/test/helpers_test.go index 895623e..a0881de 100644 --- a/test/helpers_test.go +++ b/test/helpers_test.go @@ -83,10 +83,10 @@ func newTestDB(t *testing.T) *gorm.DB { &models.Admin{}, &models.Faculty{}, &models.Warden{}, - &models.CentreHead{}, + &models.Centrehead{}, &models.FacultyPost{}, &models.WardenPost{}, - &models.CentreHeadPost{}, + &models.CentreheadPost{}, &models.Comment{}, ); err != nil { t.Fatalf("failed to migrate schema: %v", err) @@ -130,19 +130,19 @@ func newPostRouter(db *gorm.DB, auth gin.HandlerFunc) *gin.Engine { e.POST("/api/post/faculty", auth, h.FacultyPost) e.POST("/api/post/warden", auth, h.WardenPost) - e.POST("/api/post/centre_head", auth, h.CentreHeadPost) + e.POST("/api/post/centrehead", auth, h.CentreheadPost) e.PATCH("/api/post/faculty/edit/:post_id", auth, h.FacultyPostEdit) e.PATCH("/api/post/warden/edit/:post_id", auth, h.WardenPostEdit) - e.PATCH("/api/post/centre_head/edit/:post_id", auth, h.CentreHeadPostEdit) + e.PATCH("/api/post/centrehead/edit/:post_id", auth, h.CentreheadPostEdit) e.DELETE("/api/post/faculty/delete/:post_id", auth, h.FacultyPostDelete) e.DELETE("/api/post/warden/delete/:post_id", auth, h.WardenPostDelete) - e.DELETE("/api/post/centre_head/delete/:post_id", auth, h.CentreHeadPostDelete) + e.DELETE("/api/post/centrehead/delete/:post_id", auth, h.CentreheadPostDelete) e.GET("/api/post/faculty", auth, h.GetFacultyPosts) e.GET("/api/post/warden", auth, h.GetWardenPosts) - e.GET("/api/post/centre_head", auth, h.GetCentreHeadPosts) + e.GET("/api/post/centrehead", auth, h.GetCentreheadPosts) return e } @@ -167,7 +167,7 @@ func newAuthRouter(db *gorm.DB, auth gin.HandlerFunc) *gin.Engine { e := gin.New() h := &handlers.AuthHandler{DB: db} - for _, role := range []string{"faculty", "warden", "centre_head"} { + for _, role := range []string{"faculty", "warden", "centrehead"} { g := e.Group("/api/auth/" + role) switch role { case "faculty": @@ -180,11 +180,11 @@ func newAuthRouter(db *gorm.DB, auth gin.HandlerFunc) *gin.Engine { g.POST("/login", h.WardenLogin) g.POST("/forget-password", h.WardenForgetPassword) g.PATCH("/reset-password", h.WardenResetPassword) - case "centre_head": - g.POST("/signup", h.CentreHeadSignup) - g.POST("/login", h.CentreHeadLogin) - g.POST("/forget-password", h.CentreHeadForgetPassword) - g.PATCH("/reset-password", h.CentreHeadResetPassword) + case "centrehead": + g.POST("/signup", h.CentreheadSignup) + g.POST("/login", h.CentreheadLogin) + g.POST("/forget-password", h.CentreheadForgetPassword) + g.PATCH("/reset-password", h.CentreheadResetPassword) } } @@ -287,9 +287,9 @@ func seedWarden(t *testing.T, db *gorm.DB, email string) models.Warden { return w } -func seedCentreHead(t *testing.T, db *gorm.DB, email string) models.CentreHead { +func seedCentrehead(t *testing.T, db *gorm.DB, email string) models.Centrehead { t.Helper() - ch := models.CentreHead{ + ch := models.Centrehead{ Email: email, Password: testPasswordHash, Building: models.LHC,