emqx_authz_schema_tests.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_authz_schema_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. bad_authz_type_test() ->
  19. Txt = "[{type: foobar}]",
  20. ?assertThrow(
  21. [
  22. #{
  23. reason := "unknown_authz_type",
  24. got := <<"foobar">>
  25. }
  26. ],
  27. check(Txt)
  28. ).
  29. bad_mongodb_type_test() ->
  30. Txt = "[{type: mongodb, mongo_type: foobar}]",
  31. ?assertThrow(
  32. [
  33. #{
  34. reason := "unknown_mongo_type",
  35. got := <<"foobar">>
  36. }
  37. ],
  38. check(Txt)
  39. ).
  40. missing_mongodb_type_test() ->
  41. Txt = "[{type: mongodb}]",
  42. ?assertThrow(
  43. [
  44. #{
  45. reason := "unknown_mongo_type",
  46. got := undefined
  47. }
  48. ],
  49. check(Txt)
  50. ).
  51. unknown_redis_type_test() ->
  52. Txt = "[{type: redis, redis_type: foobar}]",
  53. ?assertThrow(
  54. [
  55. #{
  56. reason := "unknown_redis_type",
  57. got := <<"foobar">>
  58. }
  59. ],
  60. check(Txt)
  61. ).
  62. missing_redis_type_test() ->
  63. Txt = "[{type: redis}]",
  64. ?assertThrow(
  65. [
  66. #{
  67. reason := "unknown_redis_type",
  68. got := undefined
  69. }
  70. ],
  71. check(Txt)
  72. ).
  73. unknown_http_method_test() ->
  74. Txt = "[{type: http, method: getx}]",
  75. ?assertThrow(
  76. [
  77. #{
  78. reason := "unknown_http_method",
  79. got := <<"getx">>
  80. }
  81. ],
  82. check(Txt)
  83. ).
  84. missing_http_method_test() ->
  85. Txt = "[{type: http, methodx: get}]",
  86. ?assertThrow(
  87. [
  88. #{
  89. reason := "unknown_http_method",
  90. got := undefined
  91. }
  92. ],
  93. check(Txt)
  94. ).
  95. check(Txt0) ->
  96. Txt = ["sources: ", Txt0],
  97. {ok, RawConf} = hocon:binary(Txt),
  98. try
  99. hocon_tconf:check_plain(schema(), RawConf, #{})
  100. catch
  101. throw:{_Schema, Errors} ->
  102. throw(Errors)
  103. end.
  104. schema() ->
  105. #{roots => emqx_authz_schema:fields("authorization")}.