Skip to content

Commit 8de4340

Browse files
committed
.
1 parent 66901ca commit 8de4340

File tree

28 files changed

+131
-99
lines changed

28 files changed

+131
-99
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ APP_WEBSERVER_SHOW_FRONTEND=1
55
APP_WEBSERVER_SHOW_SWAGGER=1
66
APP_WEBSERVER_ENABLE_SWAGGER_DEV_EP=1
77

8-
APP_DATABASE_ADDR="postgres"
8+
# APP_DATABASE_ADDR="postgres"
9+
APP_DATABASE_ADDR="127.0.0.1"
910
APP_DATABASE_PORT=5432
1011
APP_DATABASE_SSL=0
1112
APP_DATABASE_USERNAME="postgres"
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
package user
22

33
import (
4+
"encoding/json"
5+
"io"
46
"net/http"
57
"testing"
68

79
"github.com/uvulpos/golang-sveltekit-template/integration-tests/helper/setup"
810
"gotest.tools/assert"
11+
12+
httpUserModel "github.com/uvulpos/golang-sveltekit-template/src/resources/user/http/http-models"
913
)
1014

1115
func TestGetUserPermissions(t *testing.T) {
1216
testData := setup.SetupTest(t)
13-
testData.AuthenticateAsUser(t, "")
17+
testData.AuthenticateAsUser(t, "TIRIEDL")
1418

15-
response := testData.MakeRequest(t, http.MethodGet, "/api/v1/self/permissions", nil)
19+
response := testData.MakeRequest(t, http.MethodGet, "/api/v1/self", nil)
1620
defer response.Body.Close()
1721

18-
assert.Equal(t, "", "", "")
22+
responseBytes, responseBytesErr := io.ReadAll(response.Body)
23+
assert.Equal(t, responseBytesErr, nil, "could not read body of http request")
24+
25+
var selfUserInformation httpUserModel.SelfInformationModel
26+
unmarshalErr := json.Unmarshal(responseBytes, &selfUserInformation)
27+
assert.Equal(t, unmarshalErr, nil, "could not unmarshal http request")
28+
29+
assert.Equal(t, selfUserInformation.Username, "TIRIEDL", "blabla")
1930
}

services/backend/integration-tests/helper/setup/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/gofiber/fiber/v2"
11+
"github.com/uvulpos/golang-sveltekit-template/src/configuration"
1112
jwtService "github.com/uvulpos/golang-sveltekit-template/src/resources/jwt/service"
1213
webApp "github.com/uvulpos/golang-sveltekit-template/src/web-app"
1314
customhttphandler "github.com/uvulpos/golang-sveltekit-template/src/web-app/custom-http-handler"
@@ -69,7 +70,7 @@ func (s *TestSetupStruct) MakeRequest(t *testing.T, applicationMethod, applicati
6970
req.AddCookie(cookie)
7071
}
7172

72-
response, responseErr := s.FiberRouter.Test(req, 1)
73+
response, responseErr := s.FiberRouter.Test(req, configuration.INTEGRATION_TEST_TEST_TIMEOUT_IN_MS)
7374
assert.Equal(t, responseErr, nil, "")
7475

7576
return response

services/backend/src/configuration/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ const (
44
CONST_APPLICATION_NAME = "Go + Svelte App"
55
CONST_APPLICATION_PATH_NAME = "gosvelte-app"
66
CONST_APPLICATION_BRANDING_HEADER = "Example Application by @uvulpos"
7+
8+
INTEGRATION_TEST_TEST_TIMEOUT_IN_MS = 5000
79
)

services/backend/src/configuration/variables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var (
99
WEBSERVER_SHOW_SWAGGER = GetEnvOrDefaultBool("WEBSERVER_SHOW_SWAGGER", true)
1010

1111
// Database
12-
// DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "127.0.0.1")
13-
DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "postgres")
12+
DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "127.0.0.1")
13+
// DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "postgres")
1414
DATABASE_PORT = GetEnvOrDefaultInt("DATABASE_PORT", 5432)
1515
DATABASE_SSL = GetEnvOrDefaultBool("DATABASE_SSL", false)
1616
DATABASE_USERNAME = GetEnvOrDefaultString("DATABASE_USERNAME", "postgres")

services/backend/src/resources/identity-provider/auth/service/callback_authentik.go renamed to services/backend/src/resources/auth/service/authentik/callback_authentik.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/go-sqlx/sqlx"
1010
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
1111
customerrorconst "github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors/custom-error-const"
12+
providerConst "github.com/uvulpos/golang-sveltekit-template/src/resources/auth/service/provider-const"
1213
)
1314

1415
func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string, customerrors.ErrorInterface) {
@@ -35,15 +36,15 @@ func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string,
3536
return "", customerrors.NewInternalServerError(err, "", "(oauth callback authentik) Failed to unmarshal authentik oauth user response body")
3637
}
3738

38-
loginUserID, loginUserError := s.storage.GetUserIDByLogin("Authentik", result.ID)
39+
loginUserID, loginUserError := s.userSvc.GetUserIDByLogin(providerConst.Authentik, result.ID)
3940
if loginUserError != nil {
4041

4142
if loginUserError.ErrorType() != customerrorconst.ERROR_IDENTIFIER_DATABASE_NOT_FOUND {
4243
return "", loginUserError
4344
}
4445

4546
// Create new user if user does not exist or relationship cannot be established
46-
tx, txErr := s.storage.StartTransaction()
47+
tx, txErr := s.userSvc.StartTransaction()
4748
if txErr != nil {
4849
return "", txErr
4950
}
@@ -52,7 +53,7 @@ func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string,
5253
tx.Rollback()
5354
}(tx)
5455

55-
createdUserID, createUserErr := s.storage.CreateUser(
56+
createdUserID, createUserErr := s.userSvc.CreateUser(
5657
tx,
5758
result.Name,
5859
result.PreferredUsername,
@@ -66,10 +67,10 @@ func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string,
6667

6768
loginUserID = createdUserID
6869

69-
createdUserLoginIdentityErr := s.storage.CreateUserLoginIdentity(
70+
createdUserLoginIdentityErr := s.userSvc.CreateUserLoginIdentity(
7071
tx,
7172
createdUserID,
72-
"Authentik",
73+
providerConst.Authentik,
7374
result.ID,
7475
)
7576
if createdUserLoginIdentityErr != nil {

services/backend/src/resources/identity-provider/auth/service/handler.go renamed to services/backend/src/resources/auth/service/authentik/handler.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,42 @@ package service
22

33
import (
44
"github.com/go-sqlx/sqlx"
5+
"github.com/uvulpos/golang-sveltekit-template/src/configuration"
56
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
67
"golang.org/x/oauth2"
8+
9+
userService "github.com/uvulpos/golang-sveltekit-template/src/resources/user/service"
710
)
811

912
type AuthService struct {
1013
authentikConfig *oauth2.Config
1114
authentikOauthUserInfoEP string
1215
authentikOauthLogoutEP string
1316

14-
storage AuthStorageInterface
17+
userSvc *userService.UserService
1518
}
1619

17-
func NewAuthService(storage AuthStorageInterface, OAuthKey, OAuthSecret, CallbackURL, AuthURL, AuthTokenURL, UserInfoURL, LogoutURL string, scope ...string) *AuthService {
20+
func NewAuthService(userSvc *userService.UserService) *AuthService {
1821
authentikConfig := &oauth2.Config{
19-
ClientID: OAuthKey,
20-
ClientSecret: OAuthSecret,
22+
ClientID: configuration.AUTHORIZATION_OAUTH_KEY,
23+
ClientSecret: configuration.AUTHORIZATION_OAUTH_SECRET,
2124

22-
RedirectURL: CallbackURL,
23-
Scopes: scope,
25+
RedirectURL: configuration.AUTHORIZATION_OAUTH_CALLBACK_URL,
26+
Scopes: configuration.AUTHORIZATION_OAUTH_SCOPES,
2427

2528
Endpoint: oauth2.Endpoint{
26-
AuthURL: AuthURL,
27-
TokenURL: AuthTokenURL,
29+
AuthURL: configuration.AUTHORIZATION_OAUTH_AUTHORIZATION_URL,
30+
TokenURL: configuration.AUTHORIZATION_OAUTH_TOKEN_URL,
2831

2932
AuthStyle: oauth2.AuthStyleInParams,
3033
},
3134
}
3235
return &AuthService{
3336
authentikConfig: authentikConfig,
34-
authentikOauthUserInfoEP: UserInfoURL,
35-
authentikOauthLogoutEP: LogoutURL,
37+
authentikOauthUserInfoEP: configuration.AUTHORIZATION_OAUTH_USERINFO_URL,
38+
authentikOauthLogoutEP: configuration.AUTHORIZATION_OAUTH_LOGOUT_URL,
3639

37-
storage: storage,
40+
userSvc: userSvc,
3841
}
3942
}
4043

0 commit comments

Comments
 (0)