This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
forked from raystack/dex
-
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
Sudheer Pal
committed
Oct 12, 2023
1 parent
d1f60e8
commit 9f3e743
Showing
7 changed files
with
165 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ func TestHandler_teamList(t *testing.T) { | |
req.Header.Add("X-Auth-Email", "[email protected]") | ||
router := chi.NewRouter() | ||
router.Use(reqctx.WithRequestCtx()) | ||
warden.Routes(nil, doer, "")(router) | ||
warden.Routes(nil, nil)(router) | ||
router.ServeHTTP(resp, req) | ||
|
||
assert.Equal(t, http.StatusOK, resp.Code) | ||
|
@@ -112,22 +112,22 @@ func TestHandler_teamList(t *testing.T) { | |
req.Header.Add("X-Auth-Email", "[email protected]") | ||
router := chi.NewRouter() | ||
router.Use(reqctx.WithRequestCtx()) | ||
warden.Routes(shieldClient, doer, "")(router) | ||
warden.Routes(shieldClient, nil)(router) | ||
router.ServeHTTP(resp, req) | ||
|
||
assert.Equal(t, http.StatusNotFound, resp.Code) | ||
}) | ||
|
||
t.Run("should return Unauthorized when X-Auth-Email is not present is header", func(t *testing.T) { | ||
shieldClient := shareMocks.NewShieldServiceClient(t) | ||
doer := mocks.NewDoer(t) | ||
// doer := mocks.NewDoer(t) | ||
|
||
resp := httptest.NewRecorder() | ||
req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, "/users/me/warden_teams", nil) | ||
require.NoError(t, err) | ||
router := chi.NewRouter() | ||
router.Use(reqctx.WithRequestCtx()) | ||
warden.Routes(shieldClient, doer, "")(router) | ||
warden.Routes(shieldClient, nil)(router) | ||
router.ServeHTTP(resp, req) | ||
|
||
assert.Equal(t, http.StatusUnauthorized, resp.Code) | ||
|
@@ -198,7 +198,7 @@ func TestHandler_updateGroup(t *testing.T) { | |
require.NoError(t, err) | ||
router := chi.NewRouter() | ||
router.Use(reqctx.WithRequestCtx()) | ||
warden.Routes(shieldClient, doer, "")(router) | ||
warden.Routes(shieldClient, nil)(router) | ||
router.ServeHTTP(resp, req) | ||
|
||
assert.Equal(t, http.StatusOK, resp.Code) | ||
|
@@ -235,22 +235,22 @@ func TestHandler_updateGroup(t *testing.T) { | |
require.NoError(t, err) | ||
router := chi.NewRouter() | ||
router.Use(reqctx.WithRequestCtx()) | ||
warden.Routes(shieldClient, doer, "")(router) | ||
warden.Routes(shieldClient, nil)(router) | ||
router.ServeHTTP(resp, req) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, resp.Code) | ||
}) | ||
|
||
t.Run("should return error when warden_team_id is empty string", func(t *testing.T) { | ||
shieldClient := shareMocks.NewShieldServiceClient(t) | ||
doer := mocks.NewDoer(t) | ||
// doer := mocks.NewDoer(t) | ||
|
||
resp := httptest.NewRecorder() | ||
req, err := http.NewRequestWithContext(context.TODO(), http.MethodPatch, "/groups/e38527ee-a8cd-40f9-98a7-1f0bbd20909f/metadata", bytes.NewBufferString(`{"warden_team_id": ""}`)) | ||
require.NoError(t, err) | ||
router := chi.NewRouter() | ||
router.Use(reqctx.WithRequestCtx()) | ||
warden.Routes(shieldClient, doer, "")(router) | ||
warden.Routes(shieldClient, nil)(router) | ||
router.ServeHTTP(resp, req) | ||
|
||
assert.Equal(t, http.StatusBadRequest, resp.Code) | ||
|
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,83 @@ | ||
package warden | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type Client struct { | ||
BaseURL string | ||
Client *http.Client | ||
} | ||
|
||
func NewClient(baseURL string) *Client { | ||
return &Client{ | ||
BaseURL: baseURL, | ||
Client: &http.Client{}, | ||
} | ||
} | ||
|
||
func (c *Client) ListUserTeams(ctx context.Context, req TeamListRequest) ([]Team, error) { | ||
const ( | ||
endpoint = "/api/v1" | ||
userPath = "/users/" | ||
teamsEndpoint = "/teams" | ||
) | ||
url := fmt.Sprintf("%s%s%s%s%s", c.BaseURL, endpoint, userPath, req.Email, teamsEndpoint) | ||
fmt.Println("URL: ", url) | ||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.Client.Do(httpReq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("failed to fetch teams: %v", resp.Status) | ||
} | ||
|
||
var data TeamListResponse | ||
err = json.NewDecoder(resp.Body).Decode(&data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data.Data.Teams, nil | ||
} | ||
|
||
func (c *Client) WardenTeamByUUID(ctx context.Context, req TeamByUUIDRequest) (*Team, error) { | ||
const ( | ||
endpoint = "/api/v2" | ||
teamPath = "/teams/" | ||
) | ||
|
||
url := fmt.Sprintf("%s%s%s%s", c.BaseURL, endpoint, teamPath, req.TeamUUID) | ||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.Client.Do(httpReq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("failed to fetch team: %v", resp.Status) | ||
} | ||
|
||
var data TeamResponse | ||
err = json.NewDecoder(resp.Body).Decode(&data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &data.Data, nil | ||
} |
Oops, something went wrong.