param_test.go 2.8 KB

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