-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3a3c4d
commit 32468fe
Showing
6 changed files
with
125 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/LitPad/backend/models" | ||
"github.com/LitPad/backend/models/choices" | ||
"github.com/LitPad/backend/schemas" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func getUsers(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string, token string) { | ||
t.Run("Reject Users Fetch Due to Invalid Account Type", func(t *testing.T) { | ||
url := fmt.Sprintf("%s?account_type=invalid-account-type", baseUrl) | ||
res := ProcessTestGetOrDelete(app, url, "GET", token) | ||
// Assert Status code | ||
assert.Equal(t, 400, res.StatusCode) | ||
|
||
// Parse and assert body | ||
body := ParseResponseBody(t, res.Body).(map[string]interface{}) | ||
assert.Equal(t, "failure", body["status"]) | ||
assert.Equal(t, "Invalid account type!", body["message"]) | ||
}) | ||
|
||
t.Run("Accept Users Fetch", func(t *testing.T) { | ||
TestVerifiedUser(db) | ||
res := ProcessTestGetOrDelete(app, baseUrl, "GET", token) | ||
// Assert Status code | ||
assert.Equal(t, 200, res.StatusCode) | ||
|
||
// Parse and assert body | ||
body := ParseResponseBody(t, res.Body).(map[string]interface{}) | ||
assert.Equal(t, "success", body["status"]) | ||
assert.Equal(t, "Profiles fetched successfully", body["message"]) | ||
}) | ||
} | ||
|
||
func updateUserRole(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string, admin models.User, token string) { | ||
data := schemas.UpdateUserRoleSchema{AccountType: choices.ACCTYPE_READER} | ||
|
||
t.Run("Reject Role Update Due to Invalid Username", func(t *testing.T) { | ||
url := fmt.Sprintf("%s/invalid-username", baseUrl) | ||
res := ProcessJsonTestBody(t, app, url, "PUT", data, token) | ||
// Assert Status code | ||
assert.Equal(t, 404, res.StatusCode) | ||
|
||
// Parse and assert body | ||
body := ParseResponseBody(t, res.Body).(map[string]interface{}) | ||
assert.Equal(t, "failure", body["status"]) | ||
assert.Equal(t, "User Not Found", body["message"]) | ||
}) | ||
|
||
t.Run("Accept Role Update Due to Valid Username", func(t *testing.T) { | ||
url := fmt.Sprintf("%s/%s", baseUrl, admin.Username) | ||
res := ProcessJsonTestBody(t, app, url, "PUT", data, token) | ||
// Assert Status code | ||
assert.Equal(t, 200, res.StatusCode) | ||
|
||
// Parse and assert body | ||
body := ParseResponseBody(t, res.Body).(map[string]interface{}) | ||
assert.Equal(t, "success", body["status"]) | ||
assert.Equal(t, "User details updated successfully!", body["message"]) | ||
}) | ||
} | ||
|
||
func toggleUserActivation(t *testing.T, app *fiber.App, db *gorm.DB, baseUrl string, token string) { | ||
user := TestUser(db) | ||
|
||
t.Run("Accept User Activation Toggle", func(t *testing.T) { | ||
url := fmt.Sprintf("%s/%s/toggle-activation", baseUrl, user.Username) | ||
res := ProcessTestGetOrDelete(app, url, "GET", token) | ||
// Assert Status code | ||
assert.Equal(t, 200, res.StatusCode) | ||
|
||
// Parse and assert body | ||
body := ParseResponseBody(t, res.Body).(map[string]interface{}) | ||
assert.Equal(t, "success", body["status"]) | ||
assert.Equal(t, "User deactivated successfully", body["message"]) | ||
}) | ||
} | ||
|
||
func TestAdminUsers(t *testing.T) { | ||
app := fiber.New() | ||
db := Setup(t, app) | ||
admin := TestAdmin(db) | ||
token := AccessToken(db, admin) | ||
baseUrl := "/api/v1/admin/users" | ||
|
||
// Run Admin Users Endpoint Tests | ||
getUsers(t, app, db, baseUrl, token) | ||
updateUserRole(t, app, db, baseUrl, admin, token) | ||
toggleUserActivation(t, app, db, baseUrl, token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,8 @@ func TestUser(db *gorm.DB) models.User { | |
db.Where("email = ?", email).Delete(&models.User{}) | ||
|
||
user := models.User{ | ||
Email: email, | ||
Password: MASTER_PASSWORD, | ||
Email: email, | ||
Password: MASTER_PASSWORD, | ||
} | ||
db.Create(&user) | ||
return user | ||
|
@@ -28,8 +28,8 @@ func TestVerifiedUser(db *gorm.DB, activeSub ...bool) models.User { | |
email := "[email protected]" | ||
|
||
user := models.User{ | ||
Email: email, | ||
Password: MASTER_PASSWORD, | ||
Email: email, | ||
Password: MASTER_PASSWORD, | ||
IsEmailVerified: true, | ||
} | ||
if len(activeSub) > 0 { | ||
|
@@ -44,10 +44,10 @@ func TestVerifiedUser(db *gorm.DB, activeSub ...bool) models.User { | |
func TestAuthor(db *gorm.DB, another ...bool) models.User { | ||
email := "[email protected]" | ||
user := models.User{ | ||
Email: email, | ||
Password: MASTER_PASSWORD, | ||
Email: email, | ||
Password: MASTER_PASSWORD, | ||
IsEmailVerified: true, | ||
AccountType: choices.ACCTYPE_AUTHOR, | ||
AccountType: choices.ACCTYPE_AUTHOR, | ||
} | ||
|
||
if len(another) > 0 { | ||
|
@@ -57,7 +57,6 @@ func TestAuthor(db *gorm.DB, another ...bool) models.User { | |
return user | ||
} | ||
|
||
|
||
func JwtData(db *gorm.DB, user models.User) models.User { | ||
access := routes.GenerateAccessToken(user.ID) | ||
refresh := routes.GenerateRefreshToken() | ||
|
@@ -125,9 +124,24 @@ func ParagraphCommentData(db *gorm.DB, chapter models.Chapter, user models.User) | |
|
||
func NotificationData(db *gorm.DB, sender models.User, receiver models.User) models.Notification { | ||
notification := models.Notification{ | ||
SenderID: sender.ID, ReceiverID: receiver.ID, | ||
SenderID: sender.ID, ReceiverID: receiver.ID, | ||
Ntype: choices.NT_FOLLOWING, Text: "This is a test notification", | ||
} | ||
db.FirstOrCreate(¬ification, notification) | ||
return notification | ||
} | ||
} | ||
|
||
// ADMIN TEST DATA | ||
func TestAdmin(db *gorm.DB) models.User { | ||
email := "[email protected]" | ||
|
||
user := models.User{ | ||
Email: email, | ||
Password: MASTER_PASSWORD, | ||
IsStaff: true, | ||
IsSuperuser: true, | ||
IsEmailVerified: true, | ||
} | ||
db.FirstOrCreate(&user, models.User{Email: user.Email}) | ||
return user | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters