result-matchers.adoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. == SecurityMockMvcResultMatchers
  2. At times it is desirable to make various security related assertions about a request.
  3. To accommodate this need, Spring Security Test support implements Spring MVC Test's `ResultMatcher` interface.
  4. In order to use Spring Security's `ResultMatcher` implementations ensure the following static import is used:
  5. [tabs]
  6. ======
  7. Java::
  8. +
  9. [source,java,role="primary"]
  10. ----
  11. import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*;
  12. ----
  13. Kotlin::
  14. +
  15. [source,kotlin,role="secondary"]
  16. ----
  17. import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*
  18. ----
  19. ======
  20. === Unauthenticated Assertion
  21. At times it may be valuable to assert that there is no authenticated user associated with the result of a `MockMvc` invocation.
  22. For example, you might want to test submitting an invalid username and password and verify that no user is authenticated.
  23. You can easily do this with Spring Security's testing support using something like the following:
  24. [tabs]
  25. ======
  26. Java::
  27. +
  28. [source,java,role="primary"]
  29. ----
  30. mvc
  31. .perform(formLogin().password("invalid"))
  32. .andExpect(unauthenticated());
  33. ----
  34. Kotlin::
  35. +
  36. [source,kotlin,role="secondary"]
  37. ----
  38. mvc
  39. .perform(formLogin().password("invalid"))
  40. .andExpect { unauthenticated() }
  41. ----
  42. ======
  43. === Authenticated Assertion
  44. It is often times that we must assert that an authenticated user exists.
  45. For example, we may want to verify that we authenticated successfully.
  46. We could verify that a form based login was successful with the following snippet of code:
  47. [tabs]
  48. ======
  49. Java::
  50. +
  51. [source,java,role="primary"]
  52. ----
  53. mvc
  54. .perform(formLogin())
  55. .andExpect(authenticated());
  56. ----
  57. Kotlin::
  58. +
  59. [source,kotlin,role="secondary"]
  60. ----
  61. mvc
  62. .perform(formLogin())
  63. .andExpect { authenticated() }
  64. ----
  65. ======
  66. If we wanted to assert the roles of the user, we could refine our previous code as shown below:
  67. [tabs]
  68. ======
  69. Java::
  70. +
  71. [source,java,role="primary"]
  72. ----
  73. mvc
  74. .perform(formLogin().user("admin"))
  75. .andExpect(authenticated().withRoles("USER","ADMIN"));
  76. ----
  77. Kotlin::
  78. +
  79. [source,kotlin,role="secondary"]
  80. ----
  81. mvc
  82. .perform(formLogin())
  83. .andExpect { authenticated().withRoles("USER","ADMIN") }
  84. ----
  85. ======
  86. Alternatively, we could verify the username:
  87. [tabs]
  88. ======
  89. Java::
  90. +
  91. [source,java,role="primary"]
  92. ----
  93. mvc
  94. .perform(formLogin().user("admin"))
  95. .andExpect(authenticated().withUsername("admin"));
  96. ----
  97. Kotlin::
  98. +
  99. [source,kotlin,role="secondary"]
  100. ----
  101. mvc
  102. .perform(formLogin().user("admin"))
  103. .andExpect { authenticated().withUsername("admin") }
  104. ----
  105. ======
  106. We can also combine the assertions:
  107. [tabs]
  108. ======
  109. Java::
  110. +
  111. [source,java,role="primary"]
  112. ----
  113. mvc
  114. .perform(formLogin().user("admin"))
  115. .andExpect(authenticated().withUsername("admin").withRoles("USER", "ADMIN"));
  116. ----
  117. Kotlin::
  118. +
  119. [source,kotlin,role="secondary"]
  120. ----
  121. mvc
  122. .perform(formLogin().user("admin"))
  123. .andExpect { authenticated().withUsername("admin").withRoles("USER", "ADMIN") }
  124. ----
  125. ======
  126. We can also make arbitrary assertions on the authentication
  127. [tabs]
  128. ======
  129. Java::
  130. +
  131. [source,java,role="primary"]
  132. ----
  133. mvc
  134. .perform(formLogin())
  135. .andExpect(authenticated().withAuthentication(auth ->
  136. assertThat(auth).isInstanceOf(UsernamePasswordAuthenticationToken.class)));
  137. ----
  138. Kotlin::
  139. +
  140. [source,kotlin,role="secondary"]
  141. ----
  142. mvc
  143. .perform(formLogin())
  144. .andExpect {
  145. authenticated().withAuthentication { auth ->
  146. assertThat(auth).isInstanceOf(UsernamePasswordAuthenticationToken::class.java) }
  147. }
  148. }
  149. ----
  150. ======