-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Partik Singh <[email protected]>
- Loading branch information
Partik Singh
authored and
Partik Singh
committed
Oct 7, 2023
1 parent
1449de3
commit fb369d1
Showing
4 changed files
with
235 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
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,90 @@ | ||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type TenantService service | ||
|
||
func (e *TenantService) CreateTenant(ctx context.Context, name string,identifier string) (JsonResponse, error) { | ||
var resp JsonResponse | ||
URL := e.client.config.BackendURL.JoinPath("tenants") | ||
n := map[string]string{"name": name,"identifier":identifier} | ||
jsonBody, _ := json.Marshal(n) | ||
b := bytes.NewBuffer(jsonBody) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), b) | ||
if err != nil { | ||
return resp, err | ||
} | ||
_, err = e.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (e *TenantService) GetTenants(ctx context.Context,page string,limit string) (JsonResponse, error) { | ||
var resp JsonResponse | ||
URL := e.client.config.BackendURL.JoinPath("tenants") | ||
v := URL.Query(); | ||
v.Set("page",page) | ||
v.Set("limit",limit) | ||
URL.RawQuery = v.Encode() | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
_, err = e.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (e *TenantService) GetTenant(ctx context.Context,identifier string) (JsonResponse, error) { | ||
var resp JsonResponse | ||
URL := e.client.config.BackendURL.JoinPath("tenants",identifier) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
_, err = e.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (e *TenantService) DeleteTenant(ctx context.Context, identifier string) (JsonResponse, error) { | ||
var resp JsonResponse | ||
URL := e.client.config.BackendURL.JoinPath("tenants", identifier) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
_, err = e.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
|
||
func (e *TenantService) UpdateTenant(ctx context.Context, identifier string,updateTenantObject *UpdateTenantRequest) (JsonResponse, error) { | ||
var resp JsonResponse | ||
URL := e.client.config.BackendURL.JoinPath("tenants", identifier) | ||
jsonBody, _ := json.Marshal(updateTenantObject) | ||
b := bytes.NewBuffer(jsonBody) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, URL.String(), b) | ||
if err != nil { | ||
return resp, err | ||
} | ||
_, err = e.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
return resp, nil | ||
} |
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,136 @@ | ||
package lib_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/novuhq/go-novu/lib" | ||
) | ||
|
||
var tenantsApiResponse = `{ | ||
"data": { | ||
"_environmentId": "string", | ||
"_id": "string", | ||
"createdAt": "string", | ||
"data": "object", | ||
"identifier": "string", | ||
"name": "string", | ||
"updatedAt": "string" | ||
} | ||
} | ||
` | ||
|
||
func TestCreateTenant(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
t.Errorf("Want POST, got %s", r.Method) | ||
} | ||
if r.URL.Path != "/v1/tenants" { | ||
t.Errorf("Want /v1/tenants, got %s", r.URL.Path) | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(tenantsApiResponse)) | ||
})) | ||
defer server.Close() | ||
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) | ||
resp, err := c.TenantApi.CreateTenant(context.Background(), "Tenant", "TenantId") | ||
if err != nil { | ||
t.Errorf("Error should be nil, got %v", err) | ||
} | ||
if resp.Data == nil || resp.Data == "" { | ||
t.Error("Expected response, got none") | ||
} | ||
} | ||
|
||
func TestGetTenants(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodGet { | ||
t.Errorf("Want GET, got %s", r.Method) | ||
} | ||
if r.URL.Path != "/v1/tenants" { | ||
t.Errorf("Want /v1/tenants, got %s", r.URL.Path) | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(tenantsApiResponse)) | ||
})) | ||
defer server.Close() | ||
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) | ||
resp, err := c.TenantApi.GetTenants(context.Background(), "1", "10") | ||
if err != nil { | ||
t.Errorf("Error should be nil, got %v", err) | ||
} | ||
if resp.Data == nil || resp.Data == "" { | ||
t.Error("Expected response, got none") | ||
} | ||
} | ||
|
||
func TestGetTenant(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodGet { | ||
t.Errorf("Want GET, got %s", r.Method) | ||
} | ||
if r.URL.Path != "/v1/tenants/TenantId" { | ||
t.Errorf("Want /v1/feeds, got %s", r.URL.Path) | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(tenantsApiResponse)) | ||
})) | ||
defer server.Close() | ||
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) | ||
resp, err := c.TenantApi.GetTenant(context.Background(), "TenantId") | ||
if err != nil { | ||
t.Errorf("Error should be nil, got %v", err) | ||
} | ||
if resp.Data == nil || resp.Data == "" { | ||
t.Error("Expected response, got none") | ||
} | ||
} | ||
|
||
func TestDeleteTenant(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodDelete { | ||
t.Errorf("Want DELETE, got %s", r.Method) | ||
} | ||
if r.URL.Path != "/v1/tenants/TenantId" { | ||
t.Errorf("Want /v1/tenants/TenantId, got %s", r.URL.Path) | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(tenantsApiResponse)) | ||
})) | ||
defer server.Close() | ||
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) | ||
resp, err := c.TenantApi.DeleteTenant(context.Background(), "TenantId") | ||
if err != nil { | ||
t.Errorf("Error should be nil, got %v", err) | ||
} | ||
if resp.Data == nil || resp.Data == "" { | ||
t.Error("Expected response, got none") | ||
} | ||
} | ||
|
||
func TestUpdateTenant(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPatch { | ||
t.Errorf("Want PATCH, got %s", r.Method) | ||
} | ||
if r.URL.Path != "/v1/tenants/TenantId" { | ||
t.Errorf("Want /v1/tenants/TenantId, got %s", r.URL.Path) | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(tenantsApiResponse)) | ||
})) | ||
defer server.Close() | ||
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) | ||
resp, err := c.TenantApi.UpdateTenant(context.Background(), "TenantId", &lib.UpdateTenantRequest{ | ||
Name: "Tenant2", | ||
}) | ||
if err != nil { | ||
t.Errorf("Error should be nil, got %v", err) | ||
} | ||
if resp.Data == nil || resp.Data == "" { | ||
t.Error("Expected response, got none") | ||
} | ||
} |