github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/service_device_policy_test.go (about)

     1  package ciossdk
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"net/url"
    10  	"testing"
    11  
    12  	srvdevice "github.com/optim-corp/cios-golang-sdk/sdk/service/device"
    13  
    14  	xmath "github.com/fcfcqloow/go-advance/math"
    15  
    16  	cnv "github.com/fcfcqloow/go-advance/convert"
    17  
    18  	"github.com/optim-corp/cios-golang-sdk/cios"
    19  
    20  	sdkmodel "github.com/optim-corp/cios-golang-sdk/model"
    21  )
    22  
    23  func TestDeviceManagement_GetPolicies(t *testing.T) {
    24  	var (
    25  		query url.Values
    26  		ctx   context.Context
    27  
    28  		tests = []struct {
    29  			params cios.ApiGetDevicePoliciesRequest
    30  			test   func()
    31  		}{
    32  			{
    33  				params: srvdevice.MakeGetPoliciesOpts().Limit(1000),
    34  				test: func() {
    35  					if query.Get("limit") != "1000" {
    36  						t.Fatal("Missing Query", query.Encode())
    37  					} else {
    38  						t.Log(query.Encode())
    39  					}
    40  				},
    41  			},
    42  			{
    43  				params: srvdevice.MakeGetPoliciesOpts().Limit(1000).Offset(50),
    44  				test: func() {
    45  					if query.Get("limit") != "1000" || query.Get("offset") != "50" {
    46  						t.Fatal("Missing Query", query.Encode())
    47  					} else {
    48  						t.Log(query.Encode())
    49  					}
    50  				},
    51  			},
    52  			{
    53  				params: srvdevice.MakeGetPoliciesOpts().ResourceOwnerId("aaaaa"),
    54  				test: func() {
    55  					if query.Get("resource_owner_id") != "aaaaa" {
    56  						t.Fatal("Missing Query", query.Encode())
    57  					} else {
    58  						t.Log(query.Encode())
    59  					}
    60  				},
    61  			},
    62  			{
    63  				params: srvdevice.MakeGetPoliciesOpts().Limit(1000).Offset(50).ResourceOwnerId("aaaaa"),
    64  				test: func() {
    65  					if query.Get("resource_owner_id") != "aaaaa" {
    66  						t.Fatal("Missing Query", query.Encode())
    67  					} else {
    68  						t.Log(query.Encode())
    69  					}
    70  				},
    71  			},
    72  			{
    73  				params: srvdevice.MakeGetPoliciesOpts().OrderBy("created_at"),
    74  				test: func() {
    75  					if query.Get("order_by") != "created_at" {
    76  						t.Fatal("Missing Query", query.Encode())
    77  					} else {
    78  						t.Log(query.Encode())
    79  					}
    80  				},
    81  			},
    82  			{
    83  				params: srvdevice.MakeGetPoliciesOpts().OrderBy("").Order("").ResourceOwnerId(""),
    84  				test: func() {
    85  					if query.Encode() != "" {
    86  						t.Fatal("Missing Query", query.Encode())
    87  					} else {
    88  						t.Log(query.Encode())
    89  					}
    90  				},
    91  			},
    92  		}
    93  	)
    94  
    95  	// Query Test
    96  	responseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    97  		query = r.URL.Query()
    98  		w.Header().Set("Content-Type", "application/json")
    99  		json.NewEncoder(w).Encode(cios.MultipleDevice{Total: 10})
   100  	})
   101  	ts := httptest.NewServer(responseHandler)
   102  	client := NewCiosClient(
   103  		CiosClientConfig{
   104  			Urls: sdkmodel.CIOSUrl{DeviceManagementUrl: ts.URL},
   105  		},
   106  	)
   107  	defer ts.Close()
   108  	for _, test := range tests {
   109  		client.DeviceManagement().GetPolicies(ctx, test.params)
   110  		test.test()
   111  	}
   112  
   113  	ts.Close()
   114  
   115  }
   116  
   117  func TestDeviceManagement_GetPoliciesAll(t *testing.T) {
   118  	var offsets []int
   119  	var limits []int
   120  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   121  		w.Header().Set("Content-Type", "application/json")
   122  		response := cios.MultipleDevicePolicy{Total: 3500}
   123  		offset := cnv.MustInt(r.URL.Query().Get("offset"))
   124  		limit := cnv.MustInt(r.URL.Query().Get("limit"))
   125  		offsets = append(offsets, offset)
   126  		limits = append(limits, limit)
   127  		for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ {
   128  			response.Policies = append(response.Policies, cios.DevicePolicy{ResourceOwnerId: cnv.MustStr(i)})
   129  		}
   130  		json.NewEncoder(w).Encode(response)
   131  	}))
   132  	defer ts.Close()
   133  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceManagementUrl: ts.URL}})
   134  
   135  	responses, _, _ := client.DeviceManagement().GetPoliciesAll(nil, srvdevice.MakeGetPoliciesOpts().Limit(999))
   136  	if len(responses) != 999 || offsets[0] != 0 && limits[0] != 1000 {
   137  		t.Fatal(len(responses))
   138  	}
   139  
   140  	offsets = []int{}
   141  	limits = []int{}
   142  	responses, _, _ = client.DeviceManagement().GetPoliciesAll(nil, srvdevice.MakeGetPoliciesOpts().Limit(1500))
   143  	if len(responses) != 1500 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 {
   144  		t.Fatal(len(responses), limits, offsets)
   145  	}
   146  	offsets = []int{}
   147  	limits = []int{}
   148  	responses, _, _ = client.DeviceManagement().GetPoliciesAll(nil, srvdevice.MakeGetPoliciesOpts().Limit(2001))
   149  	if len(responses) != 2001 || offsets[0] != 0 && limits[0] != 1000 || offsets[1] != 1000 && limits[1] != 1000 || offsets[2] != 2000 || limits[2] != 1 {
   150  		t.Fatal(len(responses), limits, offsets)
   151  
   152  	}
   153  	offsets = []int{}
   154  	limits = []int{}
   155  	responses, _, _ = client.DeviceManagement().GetPoliciesAll(nil, srvdevice.MakeGetPoliciesOpts().Limit(3501))
   156  	if len(responses) != 3500 ||
   157  		offsets[0] != 0 || limits[0] != 1000 ||
   158  		offsets[1] != 1000 && limits[1] != 1000 ||
   159  		offsets[2] != 2000 || limits[2] != 1000 ||
   160  		offsets[3] != 3000 || limits[3] != 501 {
   161  		t.Fatal(len(responses), limits, offsets)
   162  	}
   163  	offsets = []int{}
   164  	limits = []int{}
   165  	responses, _, _ = client.DeviceManagement().GetPoliciesAll(nil, srvdevice.MakeGetPoliciesOpts().Limit(2001).Offset(20))
   166  	if len(responses) != 2001 || offsets[0] != 20 && limits[0] != 1000 || offsets[1] != 1020 && limits[1] != 1000 || offsets[2] != 2020 || limits[2] != 1 {
   167  		t.Fatal(len(responses), limits, offsets)
   168  
   169  	}
   170  }
   171  
   172  func TestDeviceManagement_GetPoliciesUnlimited(t *testing.T) {
   173  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   174  		w.Header().Set("Content-Type", "application/json")
   175  		response := cios.MultipleDevicePolicy{Total: 3500, Policies: []cios.DevicePolicy{}}
   176  		offset := cnv.MustInt(r.URL.Query().Get("offset"))
   177  		limit := cnv.MustInt(r.URL.Query().Get("limit"))
   178  		for i := 0; i < xmath.MinInt(3500-offset, 1000, limit); i++ {
   179  			response.Policies = append(response.Policies, cios.DevicePolicy{ResourceOwnerId: cnv.MustStr(i)})
   180  		}
   181  		json.NewEncoder(w).Encode(response)
   182  	}))
   183  	defer ts.Close()
   184  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceManagementUrl: ts.URL}})
   185  
   186  	responses, _, _ := client.DeviceManagement().GetPoliciesUnlimited(nil, srvdevice.MakeGetPoliciesOpts().Limit(1))
   187  	if len(responses) != 3500 {
   188  		t.Fatal(len(responses))
   189  	}
   190  }
   191  
   192  func TestDeviceManagement_DeletePolicy(t *testing.T) {
   193  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   194  		if r.URL.Path != "/v2/devices/group_policies/id" {
   195  			t.Fatal(r.URL.Path)
   196  		}
   197  		w.Header().Set("Content-Type", "application/json")
   198  		if r.Method != "DELETE" {
   199  			t.Fatal(r.Method)
   200  		}
   201  	}))
   202  	defer ts.Close()
   203  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceManagementUrl: ts.URL}})
   204  	_, err := client.DeviceManagement().DeletePolicy(nil, "id")
   205  	if err != nil {
   206  		t.Fatal(err.Error())
   207  	}
   208  }
   209  
   210  func TestDeviceManagement_CreatePolicy(t *testing.T) {
   211  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   212  		w.Header().Set("Content-Type", "application/json")
   213  		body := cios.DevicePolicyRequest{}
   214  		byts, _ := ioutil.ReadAll(r.Body)
   215  		cnv.UnMarshalJson(byts, &body)
   216  		if body.ResourceOwnerId != "resource_owner_id" {
   217  			t.Fatal(body)
   218  		}
   219  		if r.URL.Path != "/v2/devices/group_policies" {
   220  			t.Fatal(r.URL.Path)
   221  		}
   222  		if r.Method != "POST" {
   223  			t.Fatal(r.Method)
   224  		}
   225  	}))
   226  	defer ts.Close()
   227  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{DeviceManagementUrl: ts.URL}})
   228  	client.DeviceManagement().CreatePolicy(nil, "resource_owner_id")
   229  }