-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Get information for the current session's user
- Loading branch information
Showing
3 changed files
with
223 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package aiven | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type ( | ||
// UserProfileHandler is the client which interacts with the Current session's user | ||
// on Aiven. | ||
UserProfileHandler struct { | ||
client *Client | ||
} | ||
|
||
CurrentUserProfileResponse struct { | ||
APIResponse | ||
User User `json:"user,omitempty"` | ||
} | ||
|
||
User struct { | ||
Auth []string `json:"auth,omitempty"` | ||
City *string `json:"city,omitempty"` | ||
Country string `json:"country,omitempty"` | ||
CreateTime string `json:"create_time,omitempty"` | ||
Department *string `json:"department,omitempty"` | ||
Features Features `json:"features,omitempty"` | ||
Intercom Intercom `json:"intercom,omitempty"` | ||
Invitations []interface{} `json:"invitations,omitempty"` | ||
JobTitle *string `json:"job_title,omitempty"` | ||
ManagedBySCIM bool `json:"managed_by_scim,omitempty"` | ||
ManagingOrganizationID *string `json:"managing_organization_id,omitempty"` | ||
ProjectMembership map[string]interface{} `json:"project_membership,omitempty"` | ||
ProjectMemberships map[string]interface{} `json:"project_memberships,omitempty"` | ||
Projects []interface{} `json:"projects,omitempty"` | ||
RealName string `json:"real_name,omitempty"` | ||
State string `json:"state,omitempty"` | ||
TokenValidityBegin *string `json:"token_validity_begin,omitempty"` | ||
User string `json:"user,omitempty"` | ||
UserID string `json:"user_id,omitempty"` | ||
ViewedIndicators []string `json:"viewed_indicators,omitempty"` | ||
} | ||
|
||
Features struct { | ||
FreeTierEnabled bool `json:"free_tier_enabled,omitempty"` | ||
ReferralEnabled bool `json:"referral_enabled,omitempty"` | ||
ShowConfigDetailsStep bool `json:"show_config_details_step,omitempty"` | ||
} | ||
|
||
Intercom struct { | ||
AppID string `json:"app_id,omitempty"` | ||
HMAC string `json:"hmac,omitempty"` | ||
} | ||
) | ||
|
||
// https://api.aiven.io/doc/#tag/Users/operation/UserInfo | ||
// Get information for the current session's user | ||
func (h *UserProfileHandler) Me(ctx context.Context) (*User, error) { | ||
bts, err := h.client.doGetRequest(ctx, "/me", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var r CurrentUserProfileResponse | ||
errR := checkAPIResponse(bts, &r) | ||
|
||
return &r.User, errR | ||
} |
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,155 @@ | ||
package aiven | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("UserProfileHandler", func() { | ||
var ( | ||
client *Client | ||
tearDown func() | ||
ctx context.Context | ||
handler *UserProfileHandler | ||
) | ||
|
||
BeforeEach(func() { | ||
client, tearDown = setupUserProfileHandlerTestCase() | ||
ctx = context.Background() | ||
handler = &UserProfileHandler{client: client} | ||
}) | ||
|
||
AfterEach(func() { | ||
tearDown() | ||
}) | ||
|
||
Context("Me", func() { | ||
It("should return the current user's profile", func() { | ||
expectedUser := &User{ | ||
Auth: []string{"auth1", "auth2"}, | ||
City: ptrToString("Helsinki"), | ||
Country: "FI", | ||
CreateTime: "2022-01-01T00:00:00Z", | ||
Department: ptrToString("Engineering"), | ||
Features: Features{FreeTierEnabled: true, ReferralEnabled: true, ShowConfigDetailsStep: true}, | ||
Intercom: Intercom{AppID: "app_id", HMAC: "hmac_value"}, | ||
Invitations: []interface{}{}, | ||
JobTitle: ptrToString("Software Engineer"), | ||
ManagedBySCIM: true, | ||
ManagingOrganizationID: ptrToString("org_id"), | ||
ProjectMembership: map[string]interface{}{"project1": "admin"}, | ||
ProjectMemberships: map[string]interface{}{"project1": "admin"}, | ||
Projects: []interface{}{"project1"}, | ||
RealName: "John Doe", | ||
State: "active", | ||
TokenValidityBegin: ptrToString("2022-01-01T00:00:00Z"), | ||
User: "john.doe", | ||
UserID: "user_id", | ||
ViewedIndicators: []string{"indicator1", "indicator2"}, | ||
} | ||
|
||
user, err := handler.Me(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(compareUsers(user, expectedUser)).To(BeTrue()) | ||
}) | ||
}) | ||
}) | ||
|
||
func setupUserProfileHandlerTestCase() (*Client, func()) { | ||
const ( | ||
UserName = "[email protected]" | ||
UserPassword = "testabcd" | ||
AccessToken = "some-random-token" | ||
) | ||
|
||
// Mock server to simulate Aiven API responses | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/me" { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
err := json.NewEncoder(w).Encode(CurrentUserProfileResponse{ | ||
APIResponse: APIResponse{}, | ||
User: User{ | ||
Auth: []string{"auth1", "auth2"}, | ||
City: ptrToString("Helsinki"), | ||
Country: "FI", | ||
CreateTime: "2022-01-01T00:00:00Z", | ||
Department: ptrToString("Engineering"), | ||
Features: Features{FreeTierEnabled: true, ReferralEnabled: true, ShowConfigDetailsStep: true}, | ||
Intercom: Intercom{AppID: "app_id", HMAC: "hmac_value"}, | ||
Invitations: []interface{}{}, | ||
JobTitle: ptrToString("Software Engineer"), | ||
ManagedBySCIM: true, | ||
ManagingOrganizationID: ptrToString("org_id"), | ||
ProjectMembership: map[string]interface{}{"project1": "admin"}, | ||
ProjectMemberships: map[string]interface{}{"project1": "admin"}, | ||
Projects: []interface{}{"project1"}, | ||
RealName: "John Doe", | ||
State: "active", | ||
TokenValidityBegin: ptrToString("2022-01-01T00:00:00Z"), | ||
User: "john.doe", | ||
UserID: "user_id", | ||
ViewedIndicators: []string{"indicator1", "indicator2"}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
})) | ||
|
||
apiUrl = ts.URL | ||
c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version()) | ||
if err != nil { | ||
Fail("user authentication error: " + err.Error()) | ||
} | ||
|
||
return c, func() { | ||
ts.Close() | ||
} | ||
} | ||
|
||
func ptrToString(s string) *string { | ||
return &s | ||
} | ||
|
||
func derefStringPtr(ptr *string) string { | ||
if ptr != nil { | ||
return *ptr | ||
} | ||
return "" | ||
} | ||
|
||
func compareUsers(got, want *User) bool { | ||
if !reflect.DeepEqual(got.Auth, want.Auth) || | ||
derefStringPtr(got.City) != derefStringPtr(want.City) || | ||
got.Country != want.Country || | ||
got.CreateTime != want.CreateTime || | ||
derefStringPtr(got.Department) != derefStringPtr(want.Department) || | ||
!reflect.DeepEqual(got.Features, want.Features) || | ||
!reflect.DeepEqual(got.Intercom, want.Intercom) || | ||
!reflect.DeepEqual(got.Invitations, want.Invitations) || | ||
derefStringPtr(got.JobTitle) != derefStringPtr(want.JobTitle) || | ||
got.ManagedBySCIM != want.ManagedBySCIM || | ||
derefStringPtr(got.ManagingOrganizationID) != derefStringPtr(want.ManagingOrganizationID) || | ||
!reflect.DeepEqual(got.ProjectMembership, want.ProjectMembership) || | ||
!reflect.DeepEqual(got.ProjectMemberships, want.ProjectMemberships) || | ||
!reflect.DeepEqual(got.Projects, want.Projects) || | ||
got.RealName != want.RealName || | ||
got.State != want.State || | ||
derefStringPtr(got.TokenValidityBegin) != derefStringPtr(want.TokenValidityBegin) || | ||
got.User != want.User || | ||
got.UserID != want.UserID || | ||
!reflect.DeepEqual(got.ViewedIndicators, want.ViewedIndicators) { | ||
return false | ||
} | ||
return true | ||
} |