-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add find invitation by token method (#346)
- Loading branch information
1 parent
4435b5c
commit 933a55a
Showing
4 changed files
with
152 additions
and
2 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 |
---|---|---|
|
@@ -2858,6 +2858,89 @@ func getInvitationTestHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write(body) | ||
} | ||
|
||
func TestFindInvitationByToken(t *testing.T) { | ||
tests := []struct { | ||
scenario string | ||
client *Client | ||
options FindInvitationByTokenOpts | ||
expected Invitation | ||
err bool | ||
}{ | ||
{ | ||
scenario: "Request without API Key returns an error", | ||
client: NewClient(""), | ||
err: true, | ||
}, | ||
{ | ||
scenario: "Request returns Invitation by token", | ||
client: NewClient("test"), | ||
options: FindInvitationByTokenOpts{InvitationToken: "myToken"}, | ||
expected: Invitation{ | ||
ID: "invitation_123", | ||
Email: "[email protected]", | ||
State: Pending, | ||
Token: "myToken", | ||
AcceptInvitationUrl: "https://your-app.com/invite?invitation_token=myToken", | ||
ExpiresAt: "2021-06-25T19:07:33.155Z", | ||
CreatedAt: "2021-06-25T19:07:33.155Z", | ||
UpdatedAt: "2021-06-25T19:07:33.155Z", | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.scenario, func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(findInvitationByTokenTestHandler)) | ||
defer server.Close() | ||
|
||
client := test.client | ||
client.Endpoint = server.URL | ||
client.HTTPClient = server.Client() | ||
|
||
invitation, err := client.FindInvitationByToken(context.Background(), test.options) | ||
if test.err { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, test.expected, invitation) | ||
}) | ||
} | ||
} | ||
|
||
func findInvitationByTokenTestHandler(w http.ResponseWriter, r *http.Request) { | ||
auth := r.Header.Get("Authorization") | ||
if auth != "Bearer test" { | ||
http.Error(w, "bad auth", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
var body []byte | ||
var err error | ||
|
||
if r.URL.Path == "/user_management/invitations/by_token/myToken" { | ||
invitation := Invitation{ | ||
ID: "invitation_123", | ||
Email: "[email protected]", | ||
State: Pending, | ||
Token: "myToken", | ||
AcceptInvitationUrl: "https://your-app.com/invite?invitation_token=myToken", | ||
ExpiresAt: "2021-06-25T19:07:33.155Z", | ||
CreatedAt: "2021-06-25T19:07:33.155Z", | ||
UpdatedAt: "2021-06-25T19:07:33.155Z", | ||
} | ||
body, err = json.Marshal(invitation) | ||
} | ||
|
||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(body) | ||
} | ||
|
||
func TestListInvitations(t *testing.T) { | ||
tests := []struct { | ||
scenario string | ||
|
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 |
---|---|---|
|
@@ -661,8 +661,8 @@ func TestUserManagementEnrollAuthFactor(t *testing.T) { | |
} | ||
|
||
authenticationRes, err := EnrollAuthFactor(context.Background(), EnrollAuthFactorOpts{ | ||
User: "user_01E3JC5F5Z1YJNPGVYWV9SX6GH", | ||
Type: mfa.TOTP, | ||
User: "user_01E3JC5F5Z1YJNPGVYWV9SX6GH", | ||
Type: mfa.TOTP, | ||
TOTPSecret: "testSecret", | ||
}) | ||
|
||
|
@@ -917,6 +917,32 @@ func TestUsersGetInvitation(t *testing.T) { | |
require.Equal(t, expectedResponse, getByIDRes) | ||
} | ||
|
||
func TestUsersFindInvitationByToken(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(findInvitationByTokenTestHandler)) | ||
defer server.Close() | ||
|
||
DefaultClient = mockClient(server) | ||
SetAPIKey("test") | ||
|
||
expectedResponse := Invitation{ | ||
ID: "invitation_123", | ||
Email: "[email protected]", | ||
State: Pending, | ||
Token: "myToken", | ||
AcceptInvitationUrl: "https://your-app.com/invite?invitation_token=myToken", | ||
ExpiresAt: "2021-06-25T19:07:33.155Z", | ||
CreatedAt: "2021-06-25T19:07:33.155Z", | ||
UpdatedAt: "2021-06-25T19:07:33.155Z", | ||
} | ||
|
||
getByIDRes, err := FindInvitationByToken(context.Background(), FindInvitationByTokenOpts{ | ||
InvitationToken: "myToken", | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, expectedResponse, getByIDRes) | ||
} | ||
|
||
func TestUsersListInvitations(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(listInvitationsTestHandler)) | ||
defer server.Close() | ||
|