2019-06-26 20:21:00 -08:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/mid"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/tests"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
2019-08-04 14:48:43 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/weberror"
|
2019-06-26 20:21:00 -08:00
|
|
|
"github.com/pborman/uuid"
|
|
|
|
)
|
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
// TestAccountCRUDAdmin tests all the account CRUD endpoints using an user with role admin.
|
|
|
|
func TestAccountCRUDAdmin(t *testing.T) {
|
|
|
|
defer tests.Recover(t)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
tr := roleTests[auth.RoleAdmin]
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
s := newMockSignup()
|
|
|
|
tr.Account = s.account
|
|
|
|
tr.User = s.user
|
|
|
|
tr.Token = s.token
|
|
|
|
tr.Claims = s.claims
|
|
|
|
ctx := s.context
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
// Test create.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusMethodNotAllowed
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
req := mockUserCreateRequest()
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Create %d w/role %s", expectedStatus, tr.Role),
|
|
|
|
http.MethodPost,
|
|
|
|
"/v1/accounts",
|
|
|
|
req,
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
if len(w.Body.String()) != 0 {
|
|
|
|
if diff := cmpDiff(t, w.Body.Bytes(), nil); diff {
|
|
|
|
t.Fatalf("\t%s\tReceived expected empty.", tests.Failed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected empty.", tests.Success)
|
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
// Test read.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusOK
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Read %d w/role %s", expectedStatus, tr.Role),
|
2019-06-26 20:21:00 -08:00
|
|
|
http.MethodGet,
|
2019-06-27 04:48:18 -08:00
|
|
|
fmt.Sprintf("/v1/accounts/%s", tr.Account.ID),
|
2019-06-26 20:21:00 -08:00
|
|
|
nil,
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
2019-06-27 04:48:18 -08:00
|
|
|
expectedStatus,
|
2019-06-26 20:21:00 -08:00
|
|
|
nil,
|
2019-06-27 04:48:18 -08:00
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
|
|
|
|
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
|
|
|
var actual account.AccountResponse
|
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMap := map[string]interface{}{
|
|
|
|
"updated_at": web.NewTimeResponse(ctx, tr.Account.UpdatedAt),
|
|
|
|
"id": tr.Account.ID,
|
|
|
|
"address2": tr.Account.Address2,
|
|
|
|
"region": tr.Account.Region,
|
|
|
|
"zipcode": tr.Account.Zipcode,
|
|
|
|
"timezone": tr.Account.Timezone,
|
|
|
|
"created_at": web.NewTimeResponse(ctx, tr.Account.CreatedAt),
|
|
|
|
"country": tr.Account.Country,
|
|
|
|
"billing_user_id": &tr.Account.BillingUserID.String,
|
|
|
|
"name": tr.Account.Name,
|
|
|
|
"address1": tr.Account.Address1,
|
|
|
|
"city": tr.Account.City,
|
|
|
|
"status": map[string]interface{}{
|
2019-08-05 17:12:28 -08:00
|
|
|
"value": "active",
|
|
|
|
"title": "Active",
|
|
|
|
"options": []map[string]interface{}{
|
|
|
|
{"selected": true, "title": "Active", "value": "active"},
|
|
|
|
{"selected": false, "title": "Pending", "value": "pending"},
|
|
|
|
{"selected": false, "title": "Disabled", "value": "disabled"},
|
|
|
|
},
|
2019-06-26 20:21:00 -08:00
|
|
|
},
|
2019-06-27 04:48:18 -08:00
|
|
|
"signup_user_id": &tr.Account.SignupUserID.String,
|
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
var expected account.AccountResponse
|
|
|
|
if err := decodeMapToStruct(expectedMap, &expected); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v\nActual results to format expected : \n", err)
|
|
|
|
printResultMap(ctx, w.Body.Bytes()) // used to help format expectedMap
|
|
|
|
t.Fatalf("\t%s\tDecode expected failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected result.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected result.", tests.Success)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Read with random ID.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusNotFound
|
|
|
|
|
|
|
|
randID := uuid.NewRandom().String()
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Read %d w/role %s using random ID", expectedStatus, tr.Role),
|
2019-06-26 20:21:00 -08:00
|
|
|
http.MethodGet,
|
2019-06-27 04:48:18 -08:00
|
|
|
fmt.Sprintf("/v1/accounts/%s", randID),
|
2019-06-26 20:21:00 -08:00
|
|
|
nil,
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
2019-06-27 04:48:18 -08:00
|
|
|
expectedStatus,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
var actual weberror.ErrorResponse
|
2019-06-27 04:48:18 -08:00
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
expected := weberror.ErrorResponse{
|
2019-08-04 14:48:43 -08:00
|
|
|
StatusCode: expectedStatus,
|
|
|
|
Error: http.StatusText(expectedStatus),
|
|
|
|
Details: fmt.Sprintf("account %s not found: Entity not found", randID),
|
|
|
|
StackTrace: actual.StackTrace,
|
2019-06-27 04:48:18 -08:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected error.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected error.", tests.Success)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Read with forbidden ID.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusNotFound
|
|
|
|
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Read %d w/role %s using forbidden ID", expectedStatus, tr.Role),
|
2019-06-26 20:21:00 -08:00
|
|
|
http.MethodGet,
|
2019-06-27 04:48:18 -08:00
|
|
|
fmt.Sprintf("/v1/accounts/%s", tr.ForbiddenAccount.ID),
|
2019-06-26 20:21:00 -08:00
|
|
|
nil,
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
2019-06-27 04:48:18 -08:00
|
|
|
expectedStatus,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
var actual weberror.ErrorResponse
|
2019-06-27 04:48:18 -08:00
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
2019-06-26 20:21:00 -08:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
expected := weberror.ErrorResponse{
|
2019-08-04 14:48:43 -08:00
|
|
|
StatusCode: expectedStatus,
|
|
|
|
Error: http.StatusText(expectedStatus),
|
|
|
|
Details: fmt.Sprintf("account %s not found: Entity not found", tr.ForbiddenAccount.ID),
|
|
|
|
StackTrace: actual.StackTrace,
|
2019-06-27 04:48:18 -08:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected error.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected error.", tests.Success)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test update.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusNoContent
|
|
|
|
|
|
|
|
newName := uuid.NewRandom().String()
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Update %d w/role %s", expectedStatus, tr.Role),
|
2019-06-26 20:21:00 -08:00
|
|
|
http.MethodPatch,
|
|
|
|
"/v1/accounts",
|
|
|
|
account.AccountUpdateRequest{
|
2019-06-27 04:48:18 -08:00
|
|
|
ID: tr.Account.ID,
|
2019-06-26 20:21:00 -08:00
|
|
|
Name: &newName,
|
|
|
|
},
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
2019-06-27 04:48:18 -08:00
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
|
|
|
if len(w.Body.String()) != 0 {
|
|
|
|
if diff := cmpDiff(t, w.Body.Bytes(), nil); diff {
|
|
|
|
t.Fatalf("\t%s\tReceived expected empty.", tests.Failed)
|
2019-06-26 20:21:00 -08:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Logf("\t%s\tReceived expected empty.", tests.Success)
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
// TestAccountCRUDUser tests all the account CRUD endpoints using an user with role user.
|
|
|
|
func TestAccountCRUDUser(t *testing.T) {
|
|
|
|
defer tests.Recover(t)
|
|
|
|
|
|
|
|
tr := roleTests[auth.RoleUser]
|
|
|
|
|
|
|
|
// Add claims to the context for the user.
|
|
|
|
ctx := context.WithValue(tests.Context(), auth.Key, tr.Claims)
|
|
|
|
|
|
|
|
// Test create.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusMethodNotAllowed
|
|
|
|
|
|
|
|
req := mockUserCreateRequest()
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Create %d w/role %s", expectedStatus, tr.Role),
|
|
|
|
http.MethodPost,
|
2019-06-26 20:21:00 -08:00
|
|
|
"/v1/accounts",
|
2019-06-27 04:48:18 -08:00
|
|
|
req,
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
|
|
|
|
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
|
|
|
if len(w.Body.String()) != 0 {
|
|
|
|
if diff := cmpDiff(t, w.Body.Bytes(), nil); diff {
|
|
|
|
t.Fatalf("\t%s\tReceived expected empty.", tests.Failed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected empty.", tests.Success)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test read.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusOK
|
|
|
|
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Read %d w/role %s", expectedStatus, tr.Role),
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf("/v1/accounts/%s", tr.Account.ID),
|
|
|
|
nil,
|
2019-06-26 20:21:00 -08:00
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
2019-06-27 04:48:18 -08:00
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
|
|
|
|
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
|
|
|
var actual account.AccountResponse
|
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMap := map[string]interface{}{
|
|
|
|
"updated_at": web.NewTimeResponse(ctx, tr.Account.UpdatedAt),
|
|
|
|
"id": tr.Account.ID,
|
|
|
|
"address2": tr.Account.Address2,
|
|
|
|
"region": tr.Account.Region,
|
|
|
|
"zipcode": tr.Account.Zipcode,
|
|
|
|
"timezone": tr.Account.Timezone,
|
|
|
|
"created_at": web.NewTimeResponse(ctx, tr.Account.CreatedAt),
|
|
|
|
"country": tr.Account.Country,
|
|
|
|
"billing_user_id": &tr.Account.BillingUserID.String,
|
|
|
|
"name": tr.Account.Name,
|
|
|
|
"address1": tr.Account.Address1,
|
|
|
|
"city": tr.Account.City,
|
|
|
|
"status": map[string]interface{}{
|
2019-08-05 17:12:28 -08:00
|
|
|
"value": "active",
|
|
|
|
"title": "Active",
|
|
|
|
"options": []map[string]interface{}{
|
|
|
|
{"selected": true, "title": "Active", "value": "active"},
|
|
|
|
{"selected": false, "title": "Pending", "value": "pending"},
|
|
|
|
{"selected": false, "title": "Disabled", "value": "disabled"},
|
|
|
|
},
|
2019-06-26 20:21:00 -08:00
|
|
|
},
|
2019-06-27 04:48:18 -08:00
|
|
|
"signup_user_id": &tr.Account.SignupUserID.String,
|
|
|
|
}
|
|
|
|
|
|
|
|
var expected account.AccountResponse
|
|
|
|
if err := decodeMapToStruct(expectedMap, &expected); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v\nActual results to format expected : \n", err)
|
|
|
|
printResultMap(ctx, w.Body.Bytes()) // used to help format expectedMap
|
|
|
|
t.Fatalf("\t%s\tDecode expected failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected result.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected result.", tests.Success)
|
2019-06-26 20:21:00 -08:00
|
|
|
}
|
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
// Test Read with random ID.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusNotFound
|
|
|
|
|
|
|
|
randID := uuid.NewRandom().String()
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Read %d w/role %s using random ID", expectedStatus, tr.Role),
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf("/v1/accounts/%s", randID),
|
|
|
|
nil,
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
|
|
|
|
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
var actual weberror.ErrorResponse
|
2019-06-27 04:48:18 -08:00
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
expected := weberror.ErrorResponse{
|
2019-08-04 14:48:43 -08:00
|
|
|
StatusCode: expectedStatus,
|
|
|
|
Error: http.StatusText(expectedStatus),
|
|
|
|
Details: fmt.Sprintf("account %s not found: Entity not found", randID),
|
|
|
|
StackTrace: actual.StackTrace,
|
2019-06-27 04:48:18 -08:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected error.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected error.", tests.Success)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Read with forbidden ID.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusNotFound
|
|
|
|
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Read %d w/role %s using forbidden ID", expectedStatus, tr.Role),
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf("/v1/accounts/%s", tr.ForbiddenAccount.ID),
|
|
|
|
nil,
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
|
|
|
nil,
|
2019-06-26 20:21:00 -08:00
|
|
|
}
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
|
|
|
|
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
var actual weberror.ErrorResponse
|
2019-06-27 04:48:18 -08:00
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
expected := weberror.ErrorResponse{
|
2019-08-04 14:48:43 -08:00
|
|
|
StatusCode: expectedStatus,
|
|
|
|
Error: http.StatusText(expectedStatus),
|
|
|
|
Details: fmt.Sprintf("account %s not found: Entity not found", tr.ForbiddenAccount.ID),
|
|
|
|
StackTrace: actual.StackTrace,
|
2019-06-27 04:48:18 -08:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected error.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected error.", tests.Success)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test update.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusForbidden
|
|
|
|
|
|
|
|
newName := uuid.NewRandom().String()
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Update %d w/role %s", expectedStatus, tr.Role),
|
2019-06-26 20:21:00 -08:00
|
|
|
http.MethodPatch,
|
|
|
|
"/v1/accounts",
|
|
|
|
account.AccountUpdateRequest{
|
2019-06-27 04:48:18 -08:00
|
|
|
ID: tr.Account.ID,
|
2019-06-26 20:21:00 -08:00
|
|
|
Name: &newName,
|
|
|
|
},
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
2019-06-27 04:48:18 -08:00
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-06-27 04:48:18 -08:00
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
var actual weberror.ErrorResponse
|
2019-06-27 04:48:18 -08:00
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
expected := mid.ErrorForbidden(ctx).(*weberror.Error).Response(ctx, false)
|
|
|
|
expected.StackTrace = actual.StackTrace
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected error.", tests.Failed)
|
2019-06-26 20:21:00 -08:00
|
|
|
}
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Logf("\t%s\tReceived expected error.", tests.Success)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAccountUpdate validates update account by ID endpoint.
|
|
|
|
func TestAccountUpdate(t *testing.T) {
|
|
|
|
defer tests.Recover(t)
|
|
|
|
|
|
|
|
tr := roleTests[auth.RoleAdmin]
|
|
|
|
|
|
|
|
// Add claims to the context for the user.
|
|
|
|
ctx := context.WithValue(tests.Context(), auth.Key, tr.Claims)
|
|
|
|
|
|
|
|
// Test create with invalid data.
|
|
|
|
{
|
|
|
|
expectedStatus := http.StatusBadRequest
|
|
|
|
|
|
|
|
invalidStatus := account.AccountStatus("invalid status")
|
|
|
|
rt := requestTest{
|
|
|
|
fmt.Sprintf("Update %d w/role %s using invalid data", expectedStatus, tr.Role),
|
2019-06-26 20:21:00 -08:00
|
|
|
http.MethodPatch,
|
|
|
|
"/v1/accounts",
|
|
|
|
account.AccountUpdateRequest{
|
2019-06-27 04:48:18 -08:00
|
|
|
ID: tr.Account.ID,
|
|
|
|
Status: &invalidStatus,
|
2019-06-26 20:21:00 -08:00
|
|
|
},
|
|
|
|
tr.Token,
|
|
|
|
tr.Claims,
|
|
|
|
expectedStatus,
|
2019-06-27 04:48:18 -08:00
|
|
|
nil,
|
|
|
|
}
|
|
|
|
t.Logf("\tTest: %s - %s %s", rt.name, rt.method, rt.url)
|
|
|
|
|
|
|
|
w, ok := executeRequestTest(t, rt, ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("\t%s\tExecute request failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived valid status code of %d.", tests.Success, w.Code)
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
var actual weberror.ErrorResponse
|
2019-06-27 04:48:18 -08:00
|
|
|
if err := json.Unmarshal(w.Body.Bytes(), &actual); err != nil {
|
|
|
|
t.Logf("\t\tGot error : %+v", err)
|
|
|
|
t.Fatalf("\t%s\tDecode response body failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
expected := weberror.ErrorResponse{
|
2019-08-04 14:48:43 -08:00
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Error: "Field validation error",
|
2019-08-01 16:17:47 -08:00
|
|
|
Fields: []weberror.FieldError{
|
|
|
|
//{Field: "status", Error: "Key: 'AccountUpdateRequest.status' Error:Field validation for 'status' failed on the 'oneof' tag"},
|
|
|
|
{
|
|
|
|
Field: "status",
|
|
|
|
Value: invalidStatus.String(),
|
|
|
|
Tag: "oneof",
|
|
|
|
Error: "status must be one of [active pending disabled]",
|
|
|
|
Display: "status must be one of [active pending disabled]",
|
|
|
|
},
|
2019-06-26 20:21:00 -08:00
|
|
|
},
|
2019-08-04 14:48:43 -08:00
|
|
|
Details: actual.Details,
|
|
|
|
StackTrace: actual.StackTrace,
|
2019-06-27 04:48:18 -08:00
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
|
2019-08-01 16:17:47 -08:00
|
|
|
if diff := cmpDiff(t, expected, actual); diff {
|
2019-06-27 04:48:18 -08:00
|
|
|
t.Fatalf("\t%s\tReceived expected error.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tReceived expected error.", tests.Success)
|
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
}
|