Skip to content

Commit

Permalink
Added support for Get information for the current session's user
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthaI committed Jul 2, 2024
1 parent 0a97dd4 commit dcca2e1
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Client struct {
Client *http.Client
UserAgent string

UserProfile *UserProfileHandler
Projects *ProjectsHandler
ProjectUsers *ProjectUsersHandler
CA *CAHandler
Expand Down Expand Up @@ -217,6 +218,7 @@ func newRetryableClient() *retryablehttp.Client {

// Init initializes the client and sets up all the handlers.
func (c *Client) Init() {
c.UserProfile = &UserProfileHandler{c}
c.Projects = &ProjectsHandler{c}
c.ProjectUsers = &ProjectUsersHandler{c}
c.CA = &CAHandler{c}
Expand Down
66 changes: 66 additions & 0 deletions me.go
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
}
155 changes: 155 additions & 0 deletions me_test.go
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
}

0 comments on commit dcca2e1

Please sign in to comment.