Oauth2StrategyTest.java 6.0 KB
Newer Older
智布道's avatar
智布道 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
智布道's avatar
智布道 已提交
16 17 18 19 20
package com.fujieid.jap.oauth2;

import com.fujieid.jap.core.JapUser;
import com.fujieid.jap.core.JapUserService;
import com.fujieid.jap.core.cache.JapCache;
21 22
import com.fujieid.jap.core.config.AuthenticateConfig;
import com.fujieid.jap.core.config.JapConfig;
智布道's avatar
智布道 已提交
23
import com.fujieid.jap.core.result.JapResponse;
24 25
import com.fujieid.jap.http.JapHttpRequest;
import com.fujieid.jap.http.JapHttpResponse;
26 27
import com.fujieid.jap.http.adapter.jakarta.JakartaRequestAdapter;
import com.fujieid.jap.http.adapter.jakarta.JakartaResponseAdapter;
智布道's avatar
智布道 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Map;

import static org.mockito.Mockito.when;

智布道's avatar
智布道 已提交
42 43 44 45 46 47 48
/**
 * unit test
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0.0
 * @since 1.0.0
 */
智布道's avatar
智布道 已提交
49 50
public class Oauth2StrategyTest {

51 52
    public JapHttpRequest request;
    public JapHttpResponse response;
智布道's avatar
智布道 已提交
53 54 55 56 57 58 59 60 61 62 63 64
    @Mock
    private HttpServletRequest httpServletRequestMock;
    @Mock
    private HttpServletResponse httpServletResponseMock;
    @Mock
    private HttpSession httpsSessionMock;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
        // Arrange
        when(httpServletRequestMock.getSession()).thenReturn(httpsSessionMock);
65 66
        this.request = new JakartaRequestAdapter(httpServletRequestMock);
        this.response = new JakartaResponseAdapter(httpServletResponseMock);
智布道's avatar
智布道 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }

    @Test
    public void constructor() {
        JapUserService japUserService = getJapUserService();
        Oauth2Strategy oauth2Strategy = new Oauth2Strategy(japUserService, new JapConfig(), new JapCache() {
            @Override
            public void set(String key, Serializable value) {

            }

            @Override
            public void set(String key, Serializable value, long timeout) {

            }

            @Override
            public Serializable get(String key) {
                return null;
            }

            @Override
            public boolean containsKey(String key) {
                return false;
            }
智布道's avatar
智布道 已提交
92 93 94 95 96 97 98 99 100 101

            /**
             * Delete the key from the cache
             *
             * @param key Cache key
             */
            @Override
            public void removeKey(String key) {

            }
智布道's avatar
智布道 已提交
102 103 104 105 106 107 108
        });
    }

    @Test
    public void authenticateNullConfig() {
        JapUserService japUserService = getJapUserService();
        Oauth2Strategy oauth2Strategy = new Oauth2Strategy(japUserService, new JapConfig());
智布道's avatar
智布道 已提交
109

110
        JapResponse response = oauth2Strategy.authenticate(null, this.request, this.response);
智布道's avatar
智布道 已提交
111
        Assert.assertEquals(1005, response.getCode());
智布道's avatar
智布道 已提交
112 113 114 115 116 117
    }

    @Test
    public void authenticateNotOAuthConfig() {
        JapUserService japUserService = getJapUserService();
        Oauth2Strategy oauth2Strategy = new Oauth2Strategy(japUserService, new JapConfig());
智布道's avatar
智布道 已提交
118

119
        JapResponse response = oauth2Strategy.authenticate(new NotOAuthConfig(), this.request, this.response);
智布道's avatar
智布道 已提交
120
        Assert.assertEquals(500, response.getCode());
智布道's avatar
智布道 已提交
121 122 123 124 125 126 127 128 129
    }

    @Test
    public void authenticate() {
        JapUserService japUserService = getJapUserService();
        Oauth2Strategy oauth2Strategy = new Oauth2Strategy(japUserService, new JapConfig());
        // Redirect to authorization url
        oauth2Strategy.authenticate(new OAuthConfig()
            .setTokenUrl("TokenUrl")
智布道's avatar
智布道 已提交
130
            .setResponseType(Oauth2ResponseType.TOKEN)
智布道's avatar
智布道 已提交
131 132 133
            .setClientSecret("ClientSecret")
            .setClientId("ClientId")
            .setAuthorizationUrl("AuthorizationUrl")
134
            .setUserinfoUrl("UserinfoUrl"), this.request, this.response);
智布道's avatar
智布道 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }

    private JapUserService getJapUserService() {
        return new JapUserService() {
            /**
             * Get user information in the current system by social platform and social user id
             * <p>
             * It is suitable for the {@code jap-social} module
             *
             * @param platform social platform,refer to {@code me.zhyd.oauth.config.AuthSource#getName()}
             * @param uid      social user id
             * @return JapUser
             */
            @Override
            public JapUser getByPlatformAndUid(String platform, String uid) {
                return new JapUser();
            }

            /**
             * Save the oauth login user information to the database and return JapUser
             * <p>
             * It is suitable for the {@code jap-oauth2} module
             *
             * @param platform  oauth2 platform name
             * @param userInfo  The basic user information returned by the OAuth platform
             * @param tokenInfo The token information returned by the OAuth platform, developers can store tokens
             *                  , type {@code com.fujieid.jap.oauth2.helper.AccessToken}
             * @return When saving successfully, return {@code JapUser}, otherwise return {@code null}
             */
            @Override
            public JapUser createAndGetOauth2User(String platform, Map<String, Object> userInfo, Object tokenInfo) {
                return new JapUser();
            }
        };
    }

    public static class NotOAuthConfig extends AuthenticateConfig {

    }

}