Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit af05292

Browse files
Ajinkya Naharajinkyan83
authored andcommitted
DA-3331: Adding org service related service methods
1 parent 7a2cfa4 commit af05292

File tree

5 files changed

+370
-0
lines changed

5 files changed

+370
-0
lines changed

orgs/dto.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package orgs
2+
3+
// Organization ...
4+
type Organization struct {
5+
ID string `json:"ID"`
6+
Name string `json:"Name"`
7+
Link string `json:"Link"`
8+
LogoURL string `json:"LogoURL"`
9+
}
10+
11+
// SearchOrganizationResponse ...
12+
type SearchOrganizationResponse struct {
13+
Data []Organization `json:"Data"`
14+
Metadata struct {
15+
Offset int `json:"Offset"`
16+
PageSize int `json:"PageSize"`
17+
TotalSize int `json:"TotalSize"`
18+
} `json:"Metadata"`
19+
}

orgs/mocks/Auth0ClientProvider.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

orgs/mocks/HTTPClientProvider.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

orgs/organization.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package orgs
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"log"
8+
"net/url"
9+
"strings"
10+
"time"
11+
12+
"github.com/LF-Engineering/dev-analytics-libraries/auth0"
13+
"github.com/LF-Engineering/dev-analytics-libraries/http"
14+
)
15+
16+
// Auth0ClientProvider ...
17+
type Auth0ClientProvider interface {
18+
ValidateToken(string) (string, error)
19+
}
20+
21+
// HTTPClientProvider ...
22+
type HTTPClientProvider interface {
23+
Request(string, string, map[string]string, []byte, map[string]string) (int, []byte, error)
24+
}
25+
26+
// Org struct
27+
type Org struct {
28+
OrgBaseURL string
29+
ESCacheURL string
30+
ESCacheUsername string
31+
ESCachePassword string
32+
AuthGrantType string
33+
AuthClientID string
34+
AuthClientSecret string
35+
AuthAudience string
36+
AuthURL string
37+
Environment string
38+
httpClient HTTPClientProvider
39+
auth0Client Auth0ClientProvider
40+
}
41+
42+
// SearchOrganization ...
43+
func (o *Org) SearchOrganization(name string, pageSize string, offset string) (*SearchOrganizationResponse, error) {
44+
if name == "" {
45+
log.Println("SearchOrganization: name param is empty")
46+
return nil, errors.New("SearchOrganization: name param is empty")
47+
}
48+
token, err := o.auth0Client.ValidateToken(o.Environment)
49+
if err != nil {
50+
log.Println(err)
51+
return nil, err
52+
}
53+
headers := make(map[string]string, 0)
54+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
55+
56+
if offset == "" {
57+
offset = "0"
58+
}
59+
60+
if pageSize == "" {
61+
pageSize = "100"
62+
}
63+
endpoint := o.OrgBaseURL + "/orgs/search?name=" + url.PathEscape(name) + "&pageSize=" + url.PathEscape(pageSize) + "&offset=" + url.PathEscape(offset)
64+
_, res, err := o.httpClient.Request(strings.TrimSpace(endpoint), "GET", headers, nil, nil)
65+
if err != nil {
66+
log.Println("SearchOrganization: Could not get the organization list: ", err)
67+
return nil, err
68+
}
69+
var response SearchOrganizationResponse
70+
err = json.Unmarshal(res, &response)
71+
if err != nil {
72+
log.Println("SearchOrganization: failed to unmarshal SearchOrganizationResponse: ", err)
73+
return nil, err
74+
}
75+
return &response, nil
76+
}
77+
78+
// LookupOrganization ...
79+
func (o *Org) LookupOrganization(name string) (*Organization, error) {
80+
if name == "" {
81+
log.Println("LookupOrganization: name param is empty")
82+
return nil, errors.New("LookupOrganization: name param is empty")
83+
}
84+
token, err := o.auth0Client.ValidateToken(o.Environment)
85+
if err != nil {
86+
log.Println(err)
87+
return nil, err
88+
}
89+
headers := make(map[string]string, 0)
90+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
91+
92+
endpoint := o.OrgBaseURL + "/lookup?name=" + url.PathEscape(name)
93+
_, res, err := o.httpClient.Request(strings.TrimSpace(endpoint), "GET", headers, nil, nil)
94+
if err != nil {
95+
log.Println("LookupOrganization: Could not get the organization: ", err)
96+
return nil, err
97+
}
98+
99+
var response Organization
100+
err = json.Unmarshal(res, &response)
101+
if err != nil {
102+
log.Println("LookupOrganization: failed to unmarshal LookupOrganizationResponse: ", err)
103+
return nil, err
104+
}
105+
return &response, nil
106+
}
107+
108+
// NewClient consumes
109+
// orgBaseURL, esCacheUrl, esCacheUsername, esCachePassword, esCacheIndex, env, authGrantType, authClientID, authClientSecret, authAudience, authURL
110+
func NewClient(orgBaseURL, esCacheURL, esCacheUsername,
111+
esCachePassword, env, authGrantType, authClientID, authClientSecret,
112+
authAudience, authURL string) (*Org, error) {
113+
org := &Org{
114+
OrgBaseURL: orgBaseURL,
115+
ESCacheURL: esCacheURL,
116+
ESCacheUsername: esCacheUsername,
117+
ESCachePassword: esCachePassword,
118+
AuthGrantType: authGrantType,
119+
AuthClientID: authClientID,
120+
AuthClientSecret: authClientSecret,
121+
AuthAudience: authAudience,
122+
AuthURL: authURL,
123+
Environment: env,
124+
}
125+
126+
httpClientProvider, auth0ClientProvider, err := buildServices(org)
127+
if err != nil {
128+
return nil, err
129+
}
130+
131+
org.httpClient = httpClientProvider
132+
org.auth0Client = auth0ClientProvider
133+
134+
return org, nil
135+
}
136+
137+
func buildServices(o *Org) (*http.ClientProvider, *auth0.ClientProvider, error) {
138+
httpClientProvider := http.NewClientProvider(time.Minute)
139+
140+
auth0ClientProvider, err := auth0.NewAuth0Client(o.ESCacheURL, o.ESCacheUsername, o.ESCachePassword, o.Environment, o.AuthGrantType, o.AuthClientID, o.AuthClientSecret, o.AuthAudience, o.AuthURL)
141+
if err != nil {
142+
return nil, nil, err
143+
}
144+
145+
return httpClientProvider, auth0ClientProvider, nil
146+
}

orgs/organization_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package orgs
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"net/url"
9+
"os"
10+
"testing"
11+
12+
"github.com/LF-Engineering/dev-analytics-libraries/orgs/mocks"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
const (
17+
OKStatus = 200
18+
)
19+
20+
var (
21+
httpClientProvider = &mocks.HTTPClientProvider{}
22+
auth0ClientProvider = &mocks.Auth0ClientProvider{}
23+
orgStruct = &Org{
24+
os.Getenv("ORG_SERVICE_ENDPOINT"),
25+
os.Getenv("ELASTIC_CACHE_URL"),
26+
os.Getenv("ELASTIC_CACHE_USERNAME"),
27+
os.Getenv("ELASTIC_CACHE_PASSWORD"),
28+
os.Getenv("AUTH0_PROD_GRANT_TYPE"),
29+
os.Getenv("AUTH0_PROD_CLIENT_ID"),
30+
os.Getenv("AUTH0_PROD_CLIENT_SECRET"),
31+
os.Getenv("AUTH0_PROD_AUDIENCE"),
32+
os.Getenv("AUTH0_TOKEN_ENDPOINT"),
33+
"test",
34+
httpClientProvider,
35+
auth0ClientProvider,
36+
}
37+
38+
token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI"
39+
orgName = "linux"
40+
)
41+
42+
func TestLookupOrganization(t *testing.T) {
43+
buf := &bytes.Buffer{}
44+
headers := make(map[string]string, 0)
45+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
46+
lookupEndpoint := orgStruct.OrgBaseURL + "/lookup?name=" + url.PathEscape(orgName)
47+
48+
data := (map[string]string{
49+
"ID": "v03fs-3",
50+
"Name": "Linux Foundation, US",
51+
"Link": "linuxfoundation.com",
52+
"LogoURL": "linuxfoundationlogo.com/logo.png",
53+
})
54+
55+
_ = json.NewEncoder(buf).Encode(data)
56+
dataBytes, _ := ioutil.ReadAll(buf)
57+
58+
auth0ClientProvider.On("ValidateToken", "test").Return(token, nil)
59+
httpClientProvider.On("Request", lookupEndpoint, "GET", headers, []byte(nil), map[string]string(nil)).Return(OKStatus, dataBytes, nil)
60+
61+
actualResponse, _ := orgStruct.LookupOrganization(orgName)
62+
assert.Equal(t, actualResponse.Name, "Linux Foundation, US")
63+
assert.Equal(t, actualResponse.ID, "v03fs-3")
64+
}
65+
66+
func TestSearchOrganization(t *testing.T) {
67+
pageSize := "100"
68+
offset := "1"
69+
70+
buf := &bytes.Buffer{}
71+
headers := make(map[string]string, 0)
72+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
73+
searchEndpoint := orgStruct.OrgBaseURL + "/orgs/search?name=" + url.PathEscape(orgName) + "&pageSize=" + pageSize + "&offset=" + offset
74+
75+
data := map[string]interface{}{
76+
"Data": []map[string]string{
77+
map[string]string{
78+
"ID": "v03fs-3",
79+
"Name": "Linux Foundation, US",
80+
"Link": "linuxfoundation.com",
81+
"LogoURL": "linuxfoundationlogo.com/logo.png",
82+
},
83+
map[string]string{
84+
"ID": "v03fs-5",
85+
"Name": "Linux Foundation, APAC",
86+
"Link": "linuxfoundation.com",
87+
"LogoURL": "linuxfoundationl.com/logo.png",
88+
},
89+
},
90+
}
91+
92+
_ = json.NewEncoder(buf).Encode(data)
93+
dataBytes, _ := ioutil.ReadAll(buf)
94+
95+
auth0ClientProvider.On("ValidateToken", "test").Return(token, nil)
96+
httpClientProvider.On("Request", searchEndpoint, "GET", headers, []byte(nil), map[string]string(nil)).Return(OKStatus, dataBytes, nil)
97+
98+
actualResponse, _ := orgStruct.SearchOrganization(orgName, pageSize, offset)
99+
assert.Equal(t, actualResponse.Data[0].Name, "Linux Foundation, US")
100+
assert.Equal(t, actualResponse.Data[0].ID, "v03fs-3")
101+
assert.Equal(t, actualResponse.Data[1].Name, "Linux Foundation, APAC")
102+
assert.Equal(t, actualResponse.Data[1].ID, "v03fs-5")
103+
}
104+
105+
func TestSearchOrganizationSpecialChars(t *testing.T) {
106+
pageSize := "100"
107+
offset := "1"
108+
orgName = "ą ę jest ż"
109+
buf := &bytes.Buffer{}
110+
headers := make(map[string]string, 0)
111+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
112+
searchEndpoint := orgStruct.OrgBaseURL + "/orgs/search?name=" + url.PathEscape(orgName) + "&pageSize=" + pageSize + "&offset=" + offset
113+
114+
data := map[string]interface{}{
115+
"Data": []map[string]string{
116+
map[string]string{
117+
"ID": "v03fs-7",
118+
"Name": "ą ę jest ż",
119+
"Link": "jest.com",
120+
"LogoURL": "jestlogo.com",
121+
},
122+
},
123+
}
124+
125+
_ = json.NewEncoder(buf).Encode(data)
126+
dataBytes, _ := ioutil.ReadAll(buf)
127+
128+
auth0ClientProvider.On("ValidateToken", "test").Return(token, nil)
129+
httpClientProvider.On("Request", searchEndpoint, "GET", headers, []byte(nil), map[string]string(nil)).Return(OKStatus, dataBytes, nil)
130+
131+
actualResponse, _ := orgStruct.SearchOrganization(orgName, pageSize, offset)
132+
assert.Equal(t, actualResponse.Data[0].Name, "ą ę jest ż")
133+
assert.Equal(t, actualResponse.Data[0].ID, "v03fs-7")
134+
}

0 commit comments

Comments
 (0)