github.com/seeker-insurance/kit@v0.0.13/web/testing/integration/assert.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/parnurzeal/gorequest"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  // AssertGetOK assert GET request is OK
    12  func AssertGetOK(t *testing.T, path string, token string) (resp gorequest.Response, body string, errs []error) {
    13  	resp, body, errs = Request("GET", path, token).End()
    14  	assert.Empty(t, errs)
    15  	AssertStatusOK(t, resp)
    16  
    17  	return resp, body, errs
    18  }
    19  
    20  // AssertAttr ...
    21  func AssertAttr(t *testing.T, i interface{}, m map[string]interface{}) {
    22  	if i == nil {
    23  		assert.True(t, false, "Expected Attributes got nil")
    24  		return
    25  	}
    26  	attrs := i.(map[string]interface{})
    27  	for k, v := range m {
    28  		assert.Equal(t, v, attrs[k])
    29  	}
    30  }
    31  
    32  // AssertLink ...
    33  func AssertLink(t *testing.T, i interface{}, names ...string) {
    34  	if i == nil {
    35  		assert.True(t, false, "Expected links got nil")
    36  		return
    37  	}
    38  	links := i.(map[string]interface{})
    39  	var link string
    40  	for _, name := range names {
    41  		if links[name] == nil {
    42  			assert.True(t, false, fmt.Sprintf("Expected links to have '%s'", name))
    43  			return
    44  		}
    45  		link = links[name].(string)
    46  		assert.True(t, len([]rune(link)) > 0)
    47  	}
    48  }
    49  
    50  // AssertNoLink ...
    51  func AssertNoLink(t *testing.T, i interface{}, names ...string) {
    52  	if i == nil {
    53  		assert.True(t, false, "Expected links got nil")
    54  		return
    55  	}
    56  	links := i.(map[string]interface{})
    57  	for _, name := range names {
    58  		if links[name] != nil {
    59  			assert.True(t, false, fmt.Sprintf("Expected links not to have '%s'", name))
    60  			return
    61  		}
    62  	}
    63  }
    64  
    65  // AssertAction ...
    66  func AssertAction(t *testing.T, meta JSONAPIRespMeta, names ...string) {
    67  	var includes bool
    68  	for _, name := range names {
    69  		includes = false
    70  		for _, action := range meta.Actions {
    71  			if action.Name == name {
    72  				includes = true
    73  			}
    74  		}
    75  		assert.True(t, includes)
    76  	}
    77  }
    78  
    79  // AssertNoAction ...
    80  func AssertNoAction(t *testing.T, meta JSONAPIRespMeta, names ...string) {
    81  	var includes bool
    82  	for _, name := range names {
    83  		includes = false
    84  		for _, action := range meta.Actions {
    85  			if action.Name == name {
    86  				includes = true
    87  			}
    88  		}
    89  		assert.True(t, !includes)
    90  	}
    91  }
    92  
    93  // AssertStatusSuccess assert response code is success
    94  func AssertStatusSuccess(t *testing.T, resp gorequest.Response) {
    95  	ok := resp.StatusCode >= 200 && resp.StatusCode <= 226
    96  	if !ok {
    97  		t.Errorf("Expected status 2xx but is %d\n%+v\n", resp.StatusCode, resp.Body)
    98  	}
    99  }
   100  
   101  // AssertStatusOK assert response status code is OK
   102  func AssertStatusOK(t *testing.T, resp gorequest.Response) {
   103  	ok := resp.StatusCode == 200 || resp.StatusCode == 201
   104  	if !ok {
   105  		t.Errorf("Expected status 200/201 but is %d\n%+v\n", resp.StatusCode, resp.Body)
   106  	}
   107  }
   108  
   109  // AssertStatusUnauthorized assert response status code is 401
   110  func AssertStatusUnauthorized(t *testing.T, resp gorequest.Response) {
   111  	ok := resp.StatusCode == 401
   112  	if !ok {
   113  		t.Errorf("Expected status 401 but is %d", resp.StatusCode)
   114  	}
   115  }
   116  
   117  // AssertStatusNotFound assert response status code is 404
   118  func AssertStatusNotFound(t *testing.T, resp gorequest.Response) {
   119  	ok := resp.StatusCode == 404
   120  	if !ok {
   121  		t.Errorf("Expected status 404 but is %d", resp.StatusCode)
   122  	}
   123  }