emqx_authn_schema_tests.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 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_authn_schema_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. %% schema error
  19. -define(ERR(Reason), {error, Reason}).
  20. union_member_selector_mongo_test_() ->
  21. Check = fun(Txt) -> check(emqx_authn_mongodb, Txt) end,
  22. [
  23. {"unknown", fun() ->
  24. ?assertMatch(
  25. ?ERR(#{field_name := mongo_type, expected := _}),
  26. Check("{mongo_type: foobar}")
  27. )
  28. end},
  29. {"single", fun() ->
  30. ?assertMatch(
  31. ?ERR(#{matched_type := "authn:mongo_single"}),
  32. Check("{mongo_type: single}")
  33. )
  34. end},
  35. {"replica-set", fun() ->
  36. ?assertMatch(
  37. ?ERR(#{matched_type := "authn:mongo_rs"}),
  38. Check("{mongo_type: rs}")
  39. )
  40. end},
  41. {"sharded", fun() ->
  42. ?assertMatch(
  43. ?ERR(#{matched_type := "authn:mongo_sharded"}),
  44. Check("{mongo_type: sharded}")
  45. )
  46. end}
  47. ].
  48. union_member_selector_jwt_test_() ->
  49. Check = fun(Txt) -> check(emqx_authn_jwt, Txt) end,
  50. [
  51. {"unknown", fun() ->
  52. ?assertMatch(
  53. ?ERR(#{field_name := use_jwks, expected := "true | false"}),
  54. Check("{use_jwks = 1}")
  55. )
  56. end},
  57. {"jwks", fun() ->
  58. ?assertMatch(
  59. ?ERR(#{matched_type := "authn:jwt_jwks"}),
  60. Check("{use_jwks = true}")
  61. )
  62. end},
  63. {"publick-key", fun() ->
  64. ?assertMatch(
  65. ?ERR(#{matched_type := "authn:jwt_public_key"}),
  66. Check("{use_jwks = false, public_key = 1}")
  67. )
  68. end},
  69. {"hmac-based", fun() ->
  70. ?assertMatch(
  71. ?ERR(#{matched_type := "authn:jwt_hmac"}),
  72. Check("{use_jwks = false}")
  73. )
  74. end}
  75. ].
  76. union_member_selector_redis_test_() ->
  77. Check = fun(Txt) -> check(emqx_authn_redis, Txt) end,
  78. [
  79. {"unknown", fun() ->
  80. ?assertMatch(
  81. ?ERR(#{field_name := redis_type, expected := _}),
  82. Check("{redis_type = 1}")
  83. )
  84. end},
  85. {"single", fun() ->
  86. ?assertMatch(
  87. ?ERR(#{matched_type := "authn:redis_single"}),
  88. Check("{redis_type = single}")
  89. )
  90. end},
  91. {"cluster", fun() ->
  92. ?assertMatch(
  93. ?ERR(#{matched_type := "authn:redis_cluster"}),
  94. Check("{redis_type = cluster}")
  95. )
  96. end},
  97. {"sentinel", fun() ->
  98. ?assertMatch(
  99. ?ERR(#{matched_type := "authn:redis_sentinel"}),
  100. Check("{redis_type = sentinel}")
  101. )
  102. end}
  103. ].
  104. union_member_selector_http_test_() ->
  105. Check = fun(Txt) -> check(emqx_authn_http, Txt) end,
  106. [
  107. {"unknown", fun() ->
  108. ?assertMatch(
  109. ?ERR(#{field_name := method, expected := _}),
  110. Check("{method = 1}")
  111. )
  112. end},
  113. {"get", fun() ->
  114. ?assertMatch(
  115. ?ERR(#{matched_type := "authn:http_get"}),
  116. Check("{method = get}")
  117. )
  118. end},
  119. {"post", fun() ->
  120. ?assertMatch(
  121. ?ERR(#{matched_type := "authn:http_post"}),
  122. Check("{method = post}")
  123. )
  124. end}
  125. ].
  126. check(Module, HoconConf) ->
  127. emqx_hocon:check(Module, ["authentication= ", HoconConf]).