View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6    */
7   package waffle.servlet;
8   
9   import jakarta.servlet.http.HttpServletRequest;
10  
11  import mockit.Expectations;
12  import mockit.Mocked;
13  
14  import org.junit.jupiter.api.Assertions;
15  import org.junit.jupiter.api.BeforeEach;
16  import org.junit.jupiter.api.Test;
17  
18  import waffle.windows.auth.IWindowsAccount;
19  import waffle.windows.auth.IWindowsIdentity;
20  import waffle.windows.auth.PrincipalFormat;
21  
22  /**
23   * Tests for {@link NegotiateRequestWrapper}.
24   */
25  class NegotiateRequestWrapperTest {
26  
27      /** The Constant TEST_FQN. */
28      private static final String TEST_FQN = "DOMAIN\\testuser";
29  
30      /** The windows identity. */
31      @Mocked
32      private IWindowsIdentity windowsIdentity;
33  
34      /** The request. */
35      @Mocked
36      private HttpServletRequest request;
37  
38      /** The wrapper. */
39      private NegotiateRequestWrapper wrapper;
40  
41      /** The principal. */
42      private WindowsPrincipal principal;
43  
44      /**
45       * Sets the up.
46       */
47      @BeforeEach
48      void setUp() {
49          Assertions.assertNotNull(new Expectations() {
50              {
51                  NegotiateRequestWrapperTest.this.windowsIdentity.getFqn();
52                  this.result = NegotiateRequestWrapperTest.TEST_FQN;
53                  NegotiateRequestWrapperTest.this.windowsIdentity.getGroups();
54                  this.result = new IWindowsAccount[0];
55              }
56          });
57          this.principal = new WindowsPrincipal(this.windowsIdentity, PrincipalFormat.FQN, PrincipalFormat.FQN);
58          this.wrapper = new NegotiateRequestWrapper(this.request, this.principal);
59      }
60  
61      /**
62       * Test get user principal.
63       */
64      @Test
65      void testGetUserPrincipal() {
66          Assertions.assertEquals(this.principal, this.wrapper.getUserPrincipal());
67      }
68  
69      /**
70       * Test get auth type.
71       */
72      @Test
73      void testGetAuthType() {
74          Assertions.assertEquals("NEGOTIATE", this.wrapper.getAuthType());
75      }
76  
77      /**
78       * Test get remote user.
79       */
80      @Test
81      void testGetRemoteUser() {
82          Assertions.assertEquals(NegotiateRequestWrapperTest.TEST_FQN, this.wrapper.getRemoteUser());
83      }
84  
85      /**
86       * Test is user in role with valid role.
87       */
88      @Test
89      void testIsUserInRoleValidRole() {
90          Assertions.assertTrue(this.wrapper.isUserInRole(NegotiateRequestWrapperTest.TEST_FQN));
91      }
92  
93      /**
94       * Test is user in role with unknown role.
95       */
96      @Test
97      void testIsUserInRoleUnknownRole() {
98          Assertions.assertFalse(this.wrapper.isUserInRole("DOMAIN\\unknownrole"));
99      }
100 }