James Moger
2015-10-05 92ae83de6b4f5401a1007bbb26e2f01168e9d6cb
commit | author | age
e97c01 1 /*
FB 2  * Copyright 2015 gitblit.com.
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 package com.gitblit.tests;
17
18 import org.apache.sshd.server.auth.gss.GSSAuthenticator;
19 import org.apache.sshd.server.session.ServerSession;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.mockito.stubbing.Answer;
25
26 import com.gitblit.manager.AuthenticationManager;
27 import com.gitblit.manager.IRuntimeManager;
28 import com.gitblit.manager.IUserManager;
29 import com.gitblit.models.UserModel;
92ae83 30 import com.gitblit.tests.mock.MemorySettings;
e97c01 31 import com.gitblit.transport.ssh.SshDaemonClient;
FB 32 import com.gitblit.transport.ssh.SshKrbAuthenticator;
33
34 public class SshKerberosAuthenticationTest extends GitblitUnitTest {
35
36     private static class UserModelWrapper {
37         public UserModel um;
38     }
39
40     @Test
41     public void testUserManager() {
42         IRuntimeManager rm = Mockito.mock(IRuntimeManager.class);
92ae83 43
e97c01 44         //Build an UserManager that can build a UserModel
FB 45         IUserManager im = Mockito.mock(IUserManager.class);
46         Mockito.doAnswer(new Answer<Object>() {
92ae83 47             @Override
e97c01 48             public Object answer(InvocationOnMock invocation) {
FB 49                 Object[] args = invocation.getArguments();
50                 String user = (String) args[0];
51                 return new UserModel(user);
92ae83 52             }
e97c01 53         }).when(im).getUserModel(Mockito.anyString());
FB 54
55         AuthenticationManager am = new AuthenticationManager(rm, im);
92ae83 56
JM 57         GSSAuthenticator gssAuthenticator = new SshKrbAuthenticator(new MemorySettings(), am);
e97c01 58
FB 59         ServerSession session = Mockito.mock(ServerSession.class);
60
61         //Build an SshDaemonClient that can set and get the UserModel
62         final UserModelWrapper umw = new UserModelWrapper();
63         SshDaemonClient client = Mockito.mock(SshDaemonClient.class);
64         Mockito.when(client.getUser()).thenReturn(umw.um);
65         Mockito.doAnswer(new Answer<Object>() {
92ae83 66             @Override
e97c01 67             public Object answer(InvocationOnMock invocation) {
FB 68                 Object[] args = invocation.getArguments();
69                 UserModel um = (UserModel) args[0];
70                 umw.um = um;
71                 return null;
92ae83 72             }
e97c01 73         }).when(client).setUser(Mockito.any(UserModel.class));
FB 74
75         Mockito.when(session.getAttribute(SshDaemonClient.KEY)).thenReturn(client);
76         Assert.assertTrue(gssAuthenticator.validateIdentity(session, "jhappy"));
77
78     }
79 }