param_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package authz_test
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/go-chi/chi"
  8. "github.com/porter-dev/porter/api/server/authz"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. type getURLParamTest struct {
  13. description string
  14. route string
  15. routeParams map[string]string
  16. paramReq string
  17. }
  18. type getURLParamErrTest struct {
  19. getURLParamTest
  20. expErrStr string
  21. }
  22. const urlParamNotFoundFmt = "could not find url param %s"
  23. const urlParamErrUintConvFmt = "could not convert url parameter %s to uint, got %s"
  24. var getURLUintParamErrTests = []getURLParamErrTest{
  25. {
  26. getURLParamTest: getURLParamTest{
  27. description: "should fail when not found",
  28. route: "/api",
  29. routeParams: map[string]string{},
  30. paramReq: "project_id",
  31. },
  32. expErrStr: fmt.Sprintf(urlParamNotFoundFmt, "project_id"),
  33. },
  34. {
  35. getURLParamTest: getURLParamTest{
  36. description: "should fail when not uint",
  37. route: "/api/notuint",
  38. routeParams: map[string]string{
  39. "project_id": "notuint",
  40. },
  41. paramReq: "project_id",
  42. },
  43. expErrStr: fmt.Sprintf(urlParamErrUintConvFmt, "project_id", "notuint"),
  44. },
  45. }
  46. func TestGetURLUintParamsErrors(t *testing.T) {
  47. for _, test := range getURLUintParamErrTests {
  48. r := httptest.NewRequest("POST", test.route, nil)
  49. // set the context for testing
  50. rctx := chi.NewRouteContext()
  51. routeParams := &chi.RouteParams{}
  52. for key, val := range test.routeParams {
  53. routeParams.Add(key, val)
  54. }
  55. rctx.URLParams = *routeParams
  56. r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
  57. _, err := authz.GetURLParamUint(r, test.paramReq)
  58. if err == nil {
  59. t.Fatalf("[ %s ] did not return an error when error was expected", test.description)
  60. }
  61. assert.EqualError(t, err, test.expErrStr)
  62. var expErrTarget apierrors.RequestError
  63. assert.ErrorAs(t, err, &expErrTarget)
  64. }
  65. }
  66. type getURLParamStringTest struct {
  67. getURLParamTest
  68. expStr string
  69. }
  70. func TestGetURLParamString(t *testing.T) {
  71. r := httptest.NewRequest("POST", "/api/test", nil)
  72. // set the context for testing
  73. rctx := chi.NewRouteContext()
  74. routeParams := &chi.RouteParams{}
  75. routeParams.Add("name", "test")
  76. rctx.URLParams = *routeParams
  77. r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
  78. res, err := authz.GetURLParamString(r, "name")
  79. if err != nil {
  80. t.Fatalf("[ GetURLParamString ] returneed an error when no error was expected, %v", err.Error())
  81. }
  82. assert.Equal(t, "test", res)
  83. }
  84. func TestGetURLParamUint(t *testing.T) {
  85. r := httptest.NewRequest("POST", "/api/1", nil)
  86. // set the context for testing
  87. rctx := chi.NewRouteContext()
  88. routeParams := &chi.RouteParams{}
  89. routeParams.Add("name", "1")
  90. rctx.URLParams = *routeParams
  91. r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
  92. res, err := authz.GetURLParamUint(r, "name")
  93. if err != nil {
  94. t.Fatalf("[ GetURLParamUint ] returneed an error when no error was expected, %v", err.Error())
  95. }
  96. assert.Equal(t, uint(1), res)
  97. }