|
| 1 | +package com.qcloud.weapp.test.authorization; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +import org.json.JSONException; |
| 9 | +import org.json.JSONObject; |
| 10 | +import org.junit.*; |
| 11 | + |
| 12 | +import com.qcloud.weapp.Configuration; |
| 13 | +import com.qcloud.weapp.ConfigurationException; |
| 14 | +import com.qcloud.weapp.ConfigurationManager; |
| 15 | +import com.qcloud.weapp.authorization.*; |
| 16 | +import com.qcloud.weapp.test.HttpMock; |
| 17 | + |
| 18 | +@SuppressWarnings("unused") |
| 19 | +public class LoginServiceTest { |
| 20 | + |
| 21 | + private LoginServiceTestHelper helper = new LoginServiceTestHelper(); |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setup() { |
| 25 | + Configuration config = new Configuration(); |
| 26 | + config.setServerHost("test.qcloud.la"); |
| 27 | + config.setAuthServerUrl("http://127.0.0.1:10086/auth"); |
| 28 | + config.setTunnelServerUrl("http://127.0.0.1:10086/tunnel"); |
| 29 | + config.setTunnelSignatureKey("test key"); |
| 30 | + config.setNetworkTimeout(1); |
| 31 | + try { |
| 32 | + ConfigurationManager.setup(config); |
| 33 | + } catch (ConfigurationException e) { |
| 34 | + e.printStackTrace(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testLoginProcess() { |
| 40 | + HttpMock mock = helper.createLoginHttpMock("valid-code", "valid-data"); |
| 41 | + LoginService service = new LoginService(mock.request, mock.response); |
| 42 | + |
| 43 | + try { |
| 44 | + UserInfo userInfo = service.login(); |
| 45 | + assertNotNull(userInfo); |
| 46 | + } catch (IllegalArgumentException | LoginServiceException | ConfigurationException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + fail(e.getMessage()); |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + JSONObject body = new JSONObject(mock.getResponseText()); |
| 53 | + assertTrue(helper.checkBodyHasMagicId(body)); |
| 54 | + assertTrue(helper.checkBodyHasSession(body)); |
| 55 | + } catch (JSONException e) { |
| 56 | + fail("invalid response body"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testLoginProcessWithoutCodeOrData() { |
| 62 | + testLoginProcessExpectError(null, "valid-data"); |
| 63 | + testLoginProcessExpectError("valid-code", null); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testLoginProcessWithInvalidCodeOrData() { |
| 68 | + testLoginProcessExpectError("invalid-code", "valid-data"); |
| 69 | + testLoginProcessExpectError("valid-code", "invalid-data"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testLoginProcessWithServerResponseError() { |
| 74 | + testLoginProcessExpectError("expect-valid-json", "valid-data"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testLoginProcessWithServer500() { |
| 79 | + testLoginProcessExpectError("expect-500", "valid-data"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testLoginProcessWithServerTimeout() { |
| 84 | + testLoginProcessExpectError("expect-timeout", "valid-data"); |
| 85 | + } |
| 86 | + |
| 87 | + private LoginServiceException testLoginProcessExpectError(String code, String encryptData) { |
| 88 | + HttpMock mock = helper.createLoginHttpMock(code, encryptData); |
| 89 | + LoginService service = new LoginService(mock.request, mock.response); |
| 90 | + |
| 91 | + LoginServiceException errorShouldThrow = null; |
| 92 | + try { |
| 93 | + UserInfo userInfo = service.login(); |
| 94 | + } catch (LoginServiceException e) { |
| 95 | + errorShouldThrow = e; |
| 96 | + } catch (ConfigurationException e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + |
| 100 | + assertNotNull(errorShouldThrow); |
| 101 | + try { |
| 102 | + JSONObject body = new JSONObject(mock.getResponseText()); |
| 103 | + assertTrue(helper.checkBodyHasMagicId(body)); |
| 104 | + assertNotNull(body.get("error")); |
| 105 | + } catch (JSONException e) { |
| 106 | + fail("invalid response body"); |
| 107 | + } |
| 108 | + return errorShouldThrow; |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testCheck() { |
| 113 | + HttpMock mock = helper.createCheckHttpMock("valid-id", "valid-key"); |
| 114 | + LoginService service = new LoginService(mock.request, mock.response); |
| 115 | + try { |
| 116 | + UserInfo userInfo = service.check(); |
| 117 | + assertNotNull(userInfo); |
| 118 | + } catch (IllegalArgumentException | LoginServiceException | ConfigurationException e) { |
| 119 | + fail(e.getMessage()); |
| 120 | + e.printStackTrace(); |
| 121 | + } |
| 122 | + try { |
| 123 | + verify(mock.response, never()).getWriter(); |
| 124 | + } catch (IOException e) { |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testCheckWithoutIdOrSkey() { |
| 131 | + testCheckExpectError(null, "valid-key", null); |
| 132 | + testCheckExpectError("valid-id", null, null); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testCheckWithInvalidIdOrSkey() { |
| 137 | + testCheckExpectError("invalid-id", "valid-key", false); |
| 138 | + testCheckExpectError("valid-id", "invalid-key", false); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testCheckWithInvalidSession() { |
| 143 | + testCheckExpectError("expect-60011", "valid-key", true); |
| 144 | + testCheckExpectError("expect-60012", "valid-key", true); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testCheckWithServerInvalidResponse() { |
| 149 | + testCheckExpectError("expect-invalid-json", "valid-key", false); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testCheckExpectError() { |
| 154 | + testCheckExpectError("expect-500", "valid-key", false); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testCheckWithServerTimeout() { |
| 159 | + testCheckExpectError("expect-timeout", "valid-key", false); |
| 160 | + } |
| 161 | + |
| 162 | + private void testCheckExpectError(String id, String skey, Boolean expectInvalidSession) { |
| 163 | + HttpMock mock = helper.createCheckHttpMock(id, skey); |
| 164 | + LoginService service = new LoginService(mock.request, mock.response); |
| 165 | + LoginServiceException errorShouldThrow = null; |
| 166 | + |
| 167 | + try { |
| 168 | + UserInfo userInfo = service.check(); |
| 169 | + } catch (LoginServiceException e) { |
| 170 | + errorShouldThrow = e; |
| 171 | + } catch (ConfigurationException e) { |
| 172 | + e.printStackTrace(); |
| 173 | + } |
| 174 | + |
| 175 | + JSONObject body = null; |
| 176 | + try { |
| 177 | + body = new JSONObject(mock.getResponseText()); |
| 178 | + assertTrue(helper.checkBodyHasMagicId(body)); |
| 179 | + String error = body.getString("error"); |
| 180 | + if (expectInvalidSession != null) { |
| 181 | + assertEquals(error.equals("ERR_INVALID_SESSION"), expectInvalidSession); |
| 182 | + } |
| 183 | + } catch (JSONException e) { |
| 184 | + fail("响应格式错误::" + e.getMessage()); |
| 185 | + } |
| 186 | + try { |
| 187 | + verify(mock.response, times(1)).getWriter(); |
| 188 | + } catch (IOException e) { |
| 189 | + e.printStackTrace(); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments