generated from PackagrIO/goweb-template
-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
7d07157
commit 178ba18
Showing
34 changed files
with
558 additions
and
320 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/fastenhealth/fasten-onprem/backend/pkg/models" | ||
) | ||
|
||
type UserMetadata struct { | ||
FullName string `json:"full_name"` | ||
Picture string `json:"picture"` | ||
Email string `json:"email"` | ||
FullName string `json:"full_name"` | ||
Picture string `json:"picture"` | ||
Email string `json:"email"` | ||
Role models.Role `json:"role"` | ||
} |
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
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,24 @@ | ||
package _20240813222836 | ||
|
||
import ( | ||
"github.com/fastenhealth/fasten-onprem/backend/pkg/models" | ||
) | ||
|
||
type Role string | ||
|
||
const ( | ||
RoleUser Role = "user" | ||
RoleAdmin Role = "admin" | ||
) | ||
|
||
type User struct { | ||
models.ModelBase | ||
FullName string `json:"full_name"` | ||
Username string `json:"username" gorm:"unique"` | ||
Password string `json:"password"` | ||
|
||
//additional optional metadata that Fasten stores with users | ||
Picture string `json:"picture"` | ||
Email string `json:"email"` | ||
Role Role `json:"role"` | ||
} |
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,93 @@ | ||
package handler_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/fastenhealth/fasten-onprem/backend/pkg" | ||
mock_config "github.com/fastenhealth/fasten-onprem/backend/pkg/config/mock" | ||
mock_database "github.com/fastenhealth/fasten-onprem/backend/pkg/database/mock" | ||
"github.com/fastenhealth/fasten-onprem/backend/pkg/models" | ||
"github.com/fastenhealth/fasten-onprem/backend/pkg/web/handler" | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAuthSignup(t *testing.T) { | ||
// Setup | ||
gin.SetMode(gin.TestMode) | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
t.Run("First user should be assigned admin role", func(t *testing.T) { | ||
mockDB := mock_database.NewMockDatabaseRepository(mockCtrl) | ||
mockConfig := mock_config.NewMockInterface(mockCtrl) | ||
|
||
mockDB.EXPECT().GetUserCount(gomock.Any()).Return(0, nil) | ||
mockDB.EXPECT().CreateUser(gomock.Any(), gomock.Any()).Do(func(_ interface{}, user *models.User) { | ||
assert.Equal(t, "admin", user.Role) | ||
}).Return(nil) | ||
mockConfig.EXPECT().GetString("jwt.issuer.key").Return("test_key") | ||
|
||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
c.Set(pkg.ContextKeyTypeDatabase, mockDB) | ||
c.Set(pkg.ContextKeyTypeConfig, mockConfig) | ||
|
||
userWizard := models.UserWizard{ | ||
User: &models.User{ | ||
Username: "testuser", | ||
Password: "testpass", | ||
}, | ||
} | ||
jsonData, _ := json.Marshal(userWizard) | ||
c.Request, _ = http.NewRequest(http.MethodPost, "/signup", bytes.NewBuffer(jsonData)) | ||
c.Request.Header.Set("Content-Type", "application/json") | ||
|
||
handler.AuthSignup(c) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
var response map[string]interface{} | ||
err := json.Unmarshal(w.Body.Bytes(), &response) | ||
assert.NoError(t, err) | ||
assert.True(t, response["success"].(bool)) | ||
}) | ||
|
||
t.Run("Subsequent user should be assigned user role", func(t *testing.T) { | ||
mockDB := mock_database.NewMockDatabaseRepository(mockCtrl) | ||
mockConfig := mock_config.NewMockInterface(mockCtrl) | ||
|
||
mockDB.EXPECT().GetUserCount(gomock.Any()).Return(1, nil) | ||
mockDB.EXPECT().CreateUser(gomock.Any(), gomock.Any()).Do(func(_ interface{}, user *models.User) { | ||
assert.Equal(t, "user", user.Role) | ||
}).Return(nil) | ||
mockConfig.EXPECT().GetString("jwt.issuer.key").Return("test_key") | ||
|
||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
c.Set(pkg.ContextKeyTypeDatabase, mockDB) | ||
c.Set(pkg.ContextKeyTypeConfig, mockConfig) | ||
|
||
userWizard := models.UserWizard{ | ||
User: &models.User{ | ||
Username: "testuser2", | ||
Password: "testpass2", | ||
}, | ||
} | ||
jsonData, _ := json.Marshal(userWizard) | ||
c.Request, _ = http.NewRequest(http.MethodPost, "/signup", bytes.NewBuffer(jsonData)) | ||
c.Request.Header.Set("Content-Type", "application/json") | ||
|
||
handler.AuthSignup(c) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
var response map[string]interface{} | ||
err := json.Unmarshal(w.Body.Bytes(), &response) | ||
assert.NoError(t, err) | ||
assert.True(t, response["success"].(bool)) | ||
}) | ||
} |
Oops, something went wrong.