diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index f46ad399e..2f8598e13 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -35,20 +35,20 @@ jobs: uses: actions/checkout@v2 - name: e2e run: make test-e2e-server-horusec - e2e-server-keycloak: - name: e2e-server-keycloak - runs-on: ubuntu-latest - if: "!contains(github.event.head_commit.message, '[skip ci]')" - steps: - - name: Set up Go 1.14 - uses: actions/setup-go@v1 - with: - go-version: 1.14 - id: go - - name: Check out code - uses: actions/checkout@v2 - - name: e2e - run: make test-e2e-server-keycloak +# e2e-server-keycloak: +# name: e2e-server-keycloak +# runs-on: ubuntu-latest +# if: "!contains(github.event.head_commit.message, '[skip ci]')" +# steps: +# - name: Set up Go 1.14 +# uses: actions/setup-go@v1 +# with: +# go-version: 1.14 +# id: go +# - name: Check out code +# uses: actions/checkout@v2 +# - name: e2e +# run: make test-e2e-server-keycloak e2e-application-admin-horusec: name: e2e-application-admin-horusec runs-on: ubuntu-latest diff --git a/deployments/docker-compose.dev.yaml b/deployments/docker-compose.dev.yaml index f5487aa78..a2d32bb22 100644 --- a/deployments/docker-compose.dev.yaml +++ b/deployments/docker-compose.dev.yaml @@ -114,9 +114,9 @@ services: HORUS_LDAP_USERFILTER: "(uid=%s)" HORUS_LDAP_GROUPFILTER: "(memberUid=%s)" HORUSEC_LDAP_ADMIN_GROUP: "admin" - HORUSEC_ENABLE_APPLICATION_ADMIN: "false" + HORUSEC_ENABLE_APPLICATION_ADMIN: "true" HORUSEC_APPLICATION_ADMIN_DATA: "{\"username\": \"horusec-admin\", \"email\":\"horusec-admin@example.com\", \"password\":\"Devpass0*\"}" - HORUSEC_AUTH_TYPE: "ldap" + HORUSEC_AUTH_TYPE: "horusec" horusec-analytic: build: context: ../ diff --git a/development-kit/pkg/entities/auth/credentials.go b/development-kit/pkg/entities/auth/credentials.go index 686b541e2..13a6b529d 100644 --- a/development-kit/pkg/entities/auth/credentials.go +++ b/development-kit/pkg/entities/auth/credentials.go @@ -15,6 +15,7 @@ package auth import ( + "encoding/json" validation "github.com/go-ozzo/ozzo-validation/v4" ) @@ -30,3 +31,8 @@ func (c *Credentials) Validate() error { validation.Field(&c.Password, validation.Length(1, 255), validation.Required), ) } + +func (c *Credentials) ToBytes() []byte { + content, _ := json.Marshal(c) + return content +} diff --git a/development-kit/pkg/entities/auth/credentials_test.go b/development-kit/pkg/entities/auth/credentials_test.go index 5df37d416..6bd5c58ae 100644 --- a/development-kit/pkg/entities/auth/credentials_test.go +++ b/development-kit/pkg/entities/auth/credentials_test.go @@ -34,4 +34,12 @@ func TestValidateCredentials(t *testing.T) { assert.Error(t, credentials.Validate()) }) + + t.Run("Should not empty when marshal", func(t *testing.T) { + credentials := &Credentials{ + Username: "horus@test.com", + Password: "UltraSafePass", + } + assert.NotEmpty(t, credentials.ToBytes()) + }) } diff --git a/e2e/application_admin/horusec/http_test.go b/e2e/application_admin/horusec/http_test.go index 5fc52839b..f5a6b706b 100644 --- a/e2e/application_admin/horusec/http_test.go +++ b/e2e/application_admin/horusec/http_test.go @@ -5,6 +5,7 @@ package horusec import ( "fmt" accountentities "github.com/ZupIT/horusec/development-kit/pkg/entities/account" + authEntities "github.com/ZupIT/horusec/development-kit/pkg/entities/auth" "github.com/ZupIT/horusec/development-kit/pkg/utils/env" "github.com/ZupIT/horusec/development-kit/pkg/utils/logger" "github.com/golang-migrate/migrate/v4" @@ -57,10 +58,10 @@ func TestServer(t *testing.T) { t.Skip("skipping integration test") } t.Run("Should tests default auth-type (horusec) http requests in application admin enable", func(t *testing.T) { - time.Sleep(5 * time.Second) + time.Sleep(10 * time.Second) // Login with default application admin - contentLogin := Login(t, &accountentities.LoginData{ - Email: "horusec-admin@example.com", + contentLogin := Login(t, &authEntities.Credentials{ + Username: "horusec-admin@example.com", Password: "Devpass0*", }) bearerToken := contentLogin["accessToken"] @@ -99,8 +100,8 @@ func TestServer(t *testing.T) { assert.NotContains(t, allCompanies, "zup") // Login with new user - contentLoginNewUser := Login(t, &accountentities.LoginData{ - Email: "e2e@example.com", + contentLoginNewUser := Login(t, &authEntities.Credentials{ + Username: "e2e@example.com", Password: "Ch@ng3m3", }) bearerTokenNewUser := contentLoginNewUser["accessToken"] diff --git a/e2e/application_admin/horusec/requests.go b/e2e/application_admin/horusec/requests.go index 239273dc0..067445d51 100644 --- a/e2e/application_admin/horusec/requests.go +++ b/e2e/application_admin/horusec/requests.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" accountentities "github.com/ZupIT/horusec/development-kit/pkg/entities/account" + authEntities "github.com/ZupIT/horusec/development-kit/pkg/entities/auth" "github.com/stretchr/testify/assert" "net/http" "testing" @@ -23,10 +24,10 @@ func CreateAccount(t *testing.T, account *accountentities.Account) { assert.NotEmpty(t, createAccountResponse["content"]) } -func Login(t *testing.T, credentials *accountentities.LoginData) map[string]string { +func Login(t *testing.T, credentials *authEntities.Credentials) map[string]string { fmt.Println("Running test for Login") loginResp, err := http.Post( - "http://127.0.0.1:8003/api/account/login", + "http://127.0.0.1:8006/api/auth/authenticate", "text/json", bytes.NewReader(credentials.ToBytes()), ) diff --git a/e2e/deployments/docker-compose.application-admin.horusec.yaml b/e2e/deployments/docker-compose.application-admin.horusec.yaml index e92d76f4d..de115a249 100644 --- a/e2e/deployments/docker-compose.application-admin.horusec.yaml +++ b/e2e/deployments/docker-compose.application-admin.horusec.yaml @@ -5,6 +5,7 @@ services: image: postgres:12 ports: - "5432:5432" + network_mode: "host" environment: POSTGRES_PASSWORD: root POSTGRES_USER: root diff --git a/e2e/server/horusec/http_test.go b/e2e/server/horusec/http_test.go index d797c37ec..f0c8c186a 100644 --- a/e2e/server/horusec/http_test.go +++ b/e2e/server/horusec/http_test.go @@ -8,6 +8,7 @@ import ( "github.com/ZupIT/horusec/development-kit/pkg/entities/account/roles" "github.com/ZupIT/horusec/development-kit/pkg/entities/api" "github.com/ZupIT/horusec/development-kit/pkg/entities/api/dto" + authEntities "github.com/ZupIT/horusec/development-kit/pkg/entities/auth" "github.com/ZupIT/horusec/development-kit/pkg/entities/horusec" rolesEnum "github.com/ZupIT/horusec/development-kit/pkg/enums/account" horusecEnums "github.com/ZupIT/horusec/development-kit/pkg/enums/horusec" @@ -69,8 +70,8 @@ func TestServer(t *testing.T) { Username: "e2e_user", }) // TESTBOOK: Login - contentLogin := Login(t, &accountentities.LoginData{ - Email: "e2e@example.com", + contentLogin := Login(t, &authEntities.Credentials{ + Username: "e2e@example.com", Password: "Ch@ng3m3", }) bearerToken := contentLogin["accessToken"] @@ -327,8 +328,8 @@ func RunCRUDUserInCompany(t *testing.T, bearerTokenAccount1, companyID string) { } assert.NotEmpty(t, accountID) // Login with new user - contentLoginAccount2 := Login(t, &accountentities.LoginData{ - Email: account2.Email, + contentLoginAccount2 := Login(t, &authEntities.Credentials{ + Username: account2.Email, Password: account2.Password, }) bearerTokenAccount2 := contentLoginAccount2["accessToken"] @@ -402,8 +403,8 @@ func RunCRUDUserInRepository(t *testing.T, bearerTokenAccount1, companyID, repos assert.NotEmpty(t, accountID) // Login with new user - contentLoginAccount2 := Login(t, &accountentities.LoginData{ - Email: account2.Email, + contentLoginAccount2 := Login(t, &authEntities.Credentials{ + Username: account2.Email, Password: account2.Password, }) bearerTokenAccount2 := contentLoginAccount2["accessToken"] diff --git a/e2e/server/horusec/requests.go b/e2e/server/horusec/requests.go index 4ce509ce6..1d7a57842 100644 --- a/e2e/server/horusec/requests.go +++ b/e2e/server/horusec/requests.go @@ -10,6 +10,7 @@ import ( "github.com/ZupIT/horusec/development-kit/pkg/entities/account/roles" "github.com/ZupIT/horusec/development-kit/pkg/entities/api" "github.com/ZupIT/horusec/development-kit/pkg/entities/api/dto" + authEntities "github.com/ZupIT/horusec/development-kit/pkg/entities/auth" "github.com/ZupIT/horusec/development-kit/pkg/utils/http-request/client" "github.com/ZupIT/horusec/development-kit/pkg/utils/http-request/request" httpResponse "github.com/ZupIT/horusec/development-kit/pkg/utils/http-request/response" @@ -32,10 +33,10 @@ func CreateAccount(t *testing.T, account *accountentities.Account) { assert.NotEmpty(t, createAccountResponse["content"]) } -func Login(t *testing.T, credentials *accountentities.LoginData) map[string]string { +func Login(t *testing.T, credentials *authEntities.Credentials) map[string]string { fmt.Println("Running test for Login") loginResp, err := http.Post( - "http://127.0.0.1:8003/api/account/login", + "http://127.0.0.1:8006/api/auth/authenticate", "text/json", bytes.NewReader(credentials.ToBytes()), ) diff --git a/e2e/server/keycloak/http_test.go b/e2e/server/keycloak/http_test.go index ba50a5e61..99410248d 100644 --- a/e2e/server/keycloak/http_test.go +++ b/e2e/server/keycloak/http_test.go @@ -25,6 +25,8 @@ import ( "time" ) +var SecretKeyCloak = "" + func TestMain(m *testing.M) { folderOfMigration := "file://../../../development-kit/pkg/databases/relational/migration" var connectionStringDB = env.GetEnvOrDefault("HORUSEC_DATABASE_SQL_URI", "postgresql://root:root@127.0.0.1:5432/horusec_db?sslmode=disable") @@ -72,39 +74,39 @@ func TestServer(t *testing.T) { Type: "password", Value: "Ch@ng3m3", } - bearerToken := SetupKeycloakAndGetFirstAccessToken(t, user, credential) - assert.NotEmpty(t, bearerToken) - CreateUserFromKeycloakInHorusec(t, &accountentities.KeycloakToken{AccessToken: bearerToken}) + SetupKeycloak(t, user, credential) - bearerToken = LoginInKeycloak(t, user.Username, credential.Value)["access_token"].(string) + bearerToken := LoginInKeycloak(t, user.Username, credential.Value)["access_token"].(string) + CheckIfTokenIsValid(t, bearerToken, SecretKeyCloak) - fmt.Println("Waiting register token in keycloak and register new user in horusec...") - time.Sleep(3 * time.Second) + CreateUserFromKeycloakInHorusec(t, &accountentities.KeycloakToken{AccessToken: bearerToken}) // TESTBOOK: Authorize // TESTBOOK: Create, Read, Update and Delete company companyID := RunCompanyCRUD(t, bearerToken) assert.NotEmpty(t, companyID) + + + // TESTBOOK: Authorize + // TESTBOOK: Create, Read, Update and Delete users in company RunCRUDUserInCompany(t, bearerToken, companyID) }) } -func SetupKeycloakAndGetFirstAccessToken(t *testing.T, user *entities.UserRepresentation, credential *entities.UserRepresentationCredentials) string { +func SetupKeycloak(t *testing.T, user *entities.UserRepresentation, credential *entities.UserRepresentationCredentials) { responseLogin := LoginInKeycloak(t, "keycloak", "keycloak") bearerToken := "Bearer " + responseLogin["access_token"].(string) UpdateRolesToAcceptOAuth(t, bearerToken) DeleteAllUsersInKeyCloak(t, bearerToken) CreateUserInKeyCloak(t, user, credential, bearerToken) - secret := GetClientSecretInAccountClient(t, bearerToken) - assert.NotEmpty(t, secret) - StartAuthHorusecServices(t, secret) - responseLogin = LoginInKeycloak(t, user.Username, credential.Value) - return responseLogin["access_token"].(string) + SecretKeyCloak = GetClientSecretInAccountClient(t, bearerToken) + assert.NotEmpty(t, SecretKeyCloak) + StartAuthHorusecServices(t, SecretKeyCloak) } func StartAuthHorusecServices(t *testing.T, secret string) { - fmt.Println("Starting auth horusec service...") + fmt.Println("Starting Horusec-Auth container...") output, err := exec.Command("whereis", "docker-compose").Output() assert.NoError(t, err) assert.NotEmpty(t, output) @@ -116,96 +118,98 @@ func StartAuthHorusecServices(t *testing.T, secret string) { output, err = cmd.CombinedOutput() assert.NoError(t, err) assert.NotEmpty(t, output) - fmt.Println("Waiting container up...") + fmt.Println("Waiting Horusec-Auth container up...") time.Sleep(3 * time.Second) } func RunCompanyCRUD(t *testing.T, bearerToken string) string { - t.Run("Should create an company, check if it exists, update your name check if name was updated delete a company and return new company to manager in other steps", func(t *testing.T) { - companyID := server.CreateCompany(t, bearerToken, &accountentities.Company{ - Name: "zup", - }) - allCompanies := server.ReadAllCompanies(t, bearerToken, true) - assert.Contains(t, allCompanies, "zup") - server.UpdateCompany(t, bearerToken, companyID, &accountentities.Company{ - Name: "zup-1", - }) - allCompaniesUpdated := server.ReadAllCompanies(t, bearerToken, true) - assert.Contains(t, allCompaniesUpdated, "zup-1") - server.DeleteCompany(t, bearerToken, companyID) + companyID := server.CreateCompany(t, bearerToken, &accountentities.Company{ + Name: "zup", }) + allCompanies := server.ReadAllCompanies(t, bearerToken, true) + assert.Contains(t, allCompanies, "zup") + server.UpdateCompany(t, bearerToken, companyID, &accountentities.Company{ + Name: "zup-1", + }) + allCompaniesUpdated := server.ReadAllCompanies(t, bearerToken, true) + assert.Contains(t, allCompaniesUpdated, "zup-1") + server.DeleteCompany(t, bearerToken, companyID) return server.CreateCompany(t, bearerToken, &accountentities.Company{ Name: "zup", }) } func RunCRUDUserInCompany(t *testing.T, bearerTokenAccount1, companyID string) { - t.Run("Should create new user and invite to existing company with permission of the member after update your permission to admin and check if is enable view dashboard by company and remove user from company", func(t *testing.T) { - companyIDParsed, _ := uuid.Parse(companyID) + companyIDParsed, _ := uuid.Parse(companyID) + + // Add new user to invite + user := &entities.UserRepresentation{ + Username: "e2e_user_2", + Email: "e2e_2@example.com", + EmailVerified: true, + Enabled: true, + } + credential := &entities.UserRepresentationCredentials{ + Temporary: false, + Type: "password", + Value: "Ch@ng3m3", + } - // Add new user to invite - user := &entities.UserRepresentation{ - Username: "e2e_user_2", - Email: "e2e_2@example.com", - EmailVerified: true, - Enabled: true, - } - credential := &entities.UserRepresentationCredentials{ - Temporary: false, - Type: "password", - Value: "Ch@ng3m3", - } - responseLoginAdmin := LoginInKeycloak(t, "keycloak", "keycloak") - CreateUserInKeyCloak(t, user, credential, "Bearer "+responseLoginAdmin["access_token"].(string)) - responseLoginNewUser := LoginInKeycloak(t, user.Username, credential.Value) - bearerTokenAccount2 := responseLoginNewUser["access_token"].(string) - CreateUserFromKeycloakInHorusec(t, &accountentities.KeycloakToken{AccessToken: bearerTokenAccount2}) - fmt.Println("Waiting register token in keycloak and register new user in horusec...") - time.Sleep(3 * time.Second) - // Invite user to existing company - server.InviteUserToCompany(t, bearerTokenAccount1, companyID, &accountentities.InviteUser{ - Role: rolesEnum.Member, - Email: user.Email, - CompanyID: companyIDParsed, - }) - - // Check if exist two users in company - allUsersInCompany := server.ReadAllUserInCompany(t, bearerTokenAccount1, companyID) - accountRoles := []roles.AccountRole{} - assert.NoError(t, json.Unmarshal([]byte(allUsersInCompany), &accountRoles)) - assert.NotEmpty(t, accountRoles) - assert.Equal(t, 2, len(accountRoles)) - accountID := "" - for _, currentUser := range accountRoles { - if currentUser.Email == user.Email { - accountID = currentUser.AccountID.String() - } - } - assert.NotEmpty(t, accountID) + // Create second user in keycloak + responseLoginAdmin := LoginInKeycloak(t, "keycloak", "keycloak") + tokenKeycloakAdmin := "Bearer "+responseLoginAdmin["access_token"].(string) + CreateUserInKeyCloak(t, user, credential, tokenKeycloakAdmin) + + // Login in keycloak and Create user in Horusec + bearerTokenAccount2 := LoginInKeycloak(t, user.Username, credential.Value)["access_token"].(string) + CreateUserFromKeycloakInHorusec(t, &accountentities.KeycloakToken{AccessToken: bearerTokenAccount2}) - // Check if company exists to new user - allCompanies := server.ReadAllCompanies(t, bearerTokenAccount2, true) - assert.Contains(t, allCompanies, "zup") + fmt.Println("Waiting register token in keycloak and register new user in horusec...") + time.Sleep(3 * time.Second) - // Expected return unauthorized because user is not admin of company to see dashboard in company view - responseChart := server.GetChartContentWithoutTreatment(t, "total-repositories", bearerTokenAccount2, companyID, "") - assert.Equal(t, http.StatusUnauthorized, responseChart.GetStatusCode()) + // Invite user to existing company + server.InviteUserToCompany(t, bearerTokenAccount1, companyID, &accountentities.InviteUser{ + Role: rolesEnum.Member, + Email: user.Email, + CompanyID: companyIDParsed, + }) - // Update permission of new user to admin - server.UpdateUserInCompany(t, bearerTokenAccount1, companyID, accountID, &roles.AccountCompany{ - Role: rolesEnum.Admin, - }) - time.Sleep(1 * time.Second) + // Check if exist two users in company + allUsersInCompany := server.ReadAllUserInCompany(t, bearerTokenAccount1, companyID) + accountRoles := []roles.AccountRole{} + assert.NoError(t, json.Unmarshal([]byte(allUsersInCompany), &accountRoles)) + assert.NotEmpty(t, accountRoles) + assert.Equal(t, 2, len(accountRoles)) + accountID := "" + for _, currentUser := range accountRoles { + if currentUser.Email == user.Email { + accountID = currentUser.AccountID.String() + } + } + assert.NotEmpty(t, accountID) - // Expected return OK because user is authorized view dashboard in company view - responseChart = server.GetChartContentWithoutTreatment(t, "total-repositories", bearerTokenAccount2, companyID, "") - assert.Equal(t, http.StatusOK, responseChart.GetStatusCode()) + // Check if company exists to new user + allCompanies := server.ReadAllCompanies(t, bearerTokenAccount2, true) + assert.Contains(t, allCompanies, "zup") - // Expected remove user from company - server.RemoveUserInCompany(t, bearerTokenAccount1, companyID, accountID) + // Expected return unauthorized because user is not admin of company to see dashboard in company view + responseChart := server.GetChartContentWithoutTreatment(t, "total-repositories", bearerTokenAccount2, companyID, "") + assert.Equal(t, http.StatusUnauthorized, responseChart.GetStatusCode()) - // Not show company for user when get all companies - allCompanies = server.ReadAllCompanies(t, bearerTokenAccount2, false) - assert.NotContains(t, allCompanies, "zup") + // Update permission of new user to admin + server.UpdateUserInCompany(t, bearerTokenAccount1, companyID, accountID, &roles.AccountCompany{ + Role: rolesEnum.Admin, }) + time.Sleep(1 * time.Second) + + // Expected return OK because user is authorized view dashboard in company view + responseChart = server.GetChartContentWithoutTreatment(t, "total-repositories", bearerTokenAccount2, companyID, "") + assert.Equal(t, http.StatusOK, responseChart.GetStatusCode()) + + // Expected remove user from company + server.RemoveUserInCompany(t, bearerTokenAccount1, companyID, accountID) + + // Not show company for user when get all companies + allCompanies = server.ReadAllCompanies(t, bearerTokenAccount2, false) + assert.NotContains(t, allCompanies, "zup") } diff --git a/e2e/server/keycloak/requests.go b/e2e/server/keycloak/requests.go index 5a6df07f5..c41a4e17a 100644 --- a/e2e/server/keycloak/requests.go +++ b/e2e/server/keycloak/requests.go @@ -6,9 +6,11 @@ import ( "encoding/json" "fmt" "github.com/ZupIT/horusec/development-kit/pkg/entities/account" + "github.com/ZupIT/horusec/development-kit/pkg/services/keycloak" "github.com/ZupIT/horusec/e2e/server/keycloak/entities" "github.com/stretchr/testify/assert" "net/http" + "os" "strings" "testing" ) @@ -29,6 +31,26 @@ func LoginInKeycloak(t *testing.T, username, password string) map[string]interfa return response } +func LogoutUserInKeycloak(t *testing.T, bearerToken, username string) { + allUsers := ListAllUsersInKeycloak(t, bearerToken) + userID := "" + for _, user := range allUsers { + if user["username"] == username { + userID = user["id"].(string) + } + } + assert.NotEmpty(t, userID) + fmt.Println("Running test for LogoutUsersInKeycloak: " + username) + req, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1:8080/auth/admin/realms/master/users/"+userID+"/logout", nil) + req.Header.Add("Authorization", bearerToken) + req.Header.Add("Content-Type", "application/json") + httpClient := http.Client{} + resp, err := httpClient.Do(req) + assert.NoError(t, err, "LogoutUsersInKeycloak, create user error mount request") + assert.Equal(t, http.StatusNoContent, resp.StatusCode, "LogoutUsersInKeycloak create user error send request") + assert.NoError(t, resp.Body.Close()) +} + func CreateUserInKeyCloak(t *testing.T, userRepresentation *entities.UserRepresentation, credentials *entities.UserRepresentationCredentials, bearerToken string) { fmt.Println("Running test for CreateUserInKeyCloak") req, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1:8080/auth/admin/realms/master/users", bytes.NewReader(userRepresentation.ToBytes())) @@ -144,6 +166,11 @@ func UpdateRolesToAcceptOAuth(t *testing.T, bearerToken string) { client["serviceAccountsEnabled"] = true client["standardFlowEnabled"] = true client["surrogateAuthRequired"] = true + client["attributes"].(map[string]interface{})["access.token.lifespan"] = 5940 + client["attributes"].(map[string]interface{})["client.offline.session.idle.timeout"] = 5940 + client["attributes"].(map[string]interface{})["client.offline.session.max.lifespan"] = 5940 + client["attributes"].(map[string]interface{})["client.session.idle.timeout"] = 5940 + client["attributes"].(map[string]interface{})["client.session.max.lifespan"] = 5940 clientID := client["id"].(string) clientBytes, _ := json.Marshal(client) req, _ := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/auth/admin/realms/master/clients/"+clientID, bytes.NewReader(clientBytes)) @@ -231,3 +258,15 @@ func CreateUserFromKeycloakInHorusec(t *testing.T, token *account.KeycloakToken) assert.NoError(t, createCompanyResp.Body.Close()) assert.NotEmpty(t, bodyResponse) } + +func CheckIfTokenIsValid(t *testing.T, token, secret string) { + fmt.Println("Running test for CheckIfTokenIsValid") + assert.NoError(t, os.Setenv("HORUSEC_KEYCLOAK_BASE_PATH", "http://127.0.0.1:8080")) + assert.NoError(t, os.Setenv("HORUSEC_KEYCLOAK_CLIENT_ID", "account")) + assert.NoError(t, os.Setenv("HORUSEC_KEYCLOAK_CLIENT_SECRET", secret)) + assert.NoError(t, os.Setenv("HORUSEC_KEYCLOAK_REALM", "master")) + assert.NoError(t, os.Setenv("HORUSEC_KEYCLOAK_OTP", "false")) + userID, err := keycloak.NewKeycloakService().GetAccountIDByJWTToken(token) + assert.NoError(t, err) + assert.NotEmpty(t, userID) +} diff --git a/e2e/server/messages/messages_test.go b/e2e/server/messages/messages_test.go index b415b7a8f..f194fd8bb 100644 --- a/e2e/server/messages/messages_test.go +++ b/e2e/server/messages/messages_test.go @@ -4,6 +4,7 @@ package messages import ( "github.com/ZupIT/horusec/development-kit/pkg/databases/relational/adapter" + authEntities "github.com/ZupIT/horusec/development-kit/pkg/entities/auth" "github.com/ZupIT/horusec/development-kit/pkg/utils/test" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -63,9 +64,9 @@ func TestMessages(t *testing.T) { CreateAccount(t, accountToCreate) // When try login without confirm account return unauthorized - loginResp := Login(t, &accountentities.LoginData{ - Email: "e2e@example.com", - Password: "Ch@ng3m3", + loginResp := Login(t, &authEntities.Credentials{ + Username: accountToCreate.Email, + Password: accountToCreate.Password, }) assert.Equal(t, http.StatusForbidden, loginResp.GetStatusCode()) @@ -76,9 +77,9 @@ func TestMessages(t *testing.T) { ValidateAccount(t, accountCreated.AccountID.String()) // Check if is possible login now - bearerToken := LoginAndReturnAccessToken(t, &accountentities.LoginData{ - Email: "e2e@example.com", - Password: "Ch@ng3m3", + bearerToken := LoginAndReturnAccessToken(t, &authEntities.Credentials{ + Username: accountToCreate.Email, + Password: accountToCreate.Password, }) Logout(t, bearerToken) }) diff --git a/e2e/server/messages/requests.go b/e2e/server/messages/requests.go index 600163a02..daac59d60 100644 --- a/e2e/server/messages/requests.go +++ b/e2e/server/messages/requests.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" accountentities "github.com/ZupIT/horusec/development-kit/pkg/entities/account" + authEntities "github.com/ZupIT/horusec/development-kit/pkg/entities/auth" "github.com/ZupIT/horusec/development-kit/pkg/utils/http-request/client" httpResponse "github.com/ZupIT/horusec/development-kit/pkg/utils/http-request/response" "github.com/stretchr/testify/assert" @@ -27,20 +28,20 @@ func CreateAccount(t *testing.T, account *accountentities.Account) { assert.NotEmpty(t, createAccountResponse["content"]) } -func Login(t *testing.T, credentials *accountentities.LoginData) httpResponse.Interface { +func Login(t *testing.T, credentials *authEntities.Credentials) httpResponse.Interface { fmt.Println("Running test for Login") req, _ := http.NewRequest( http.MethodPost, - "http://127.0.0.1:8003/api/account/login", + "http://127.0.0.1:8006/api/auth/authenticate", bytes.NewReader(credentials.ToBytes())) res, err := client.NewHTTPClient(15).DoRequest(req, &tls.Config{}) assert.NoError(t, err) return res } -func LoginAndReturnAccessToken(t *testing.T, credentials *accountentities.LoginData) string { +func LoginAndReturnAccessToken(t *testing.T, credentials *authEntities.Credentials) string { fmt.Println("Running test for Login") loginResp, err := http.Post( - "http://127.0.0.1:8003/api/account/login", + "http://127.0.0.1:8006/api/auth/authenticate", "text/json", bytes.NewReader(credentials.ToBytes()), )