Browse Source
* add feature_10290 * fix dolphinscheduler-ui/pnpm-lock.yaml error * fix dolphinscheduler-ui/pnpm-lock.yaml error * fix incorrect dependency * fix incorrect dependency and unit test * fix unit test error * add dolphinscheduler-api/logs to .gitignore * add AccessTokenV2ControllerTest.java * fix AccessTokenV2ControllerTest error3.1.0-release
xiangzihao
3 years ago
committed by
GitHub
14 changed files with 341 additions and 24 deletions
@ -0,0 +1,76 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.apache.dolphinscheduler.api.controller; |
||||||
|
|
||||||
|
import io.swagger.annotations.Api; |
||||||
|
import io.swagger.annotations.ApiOperation; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation; |
||||||
|
import org.apache.dolphinscheduler.api.dto.CreateTokenRequest; |
||||||
|
import org.apache.dolphinscheduler.api.dto.CreateTokenResponse; |
||||||
|
import org.apache.dolphinscheduler.api.exceptions.ApiException; |
||||||
|
import org.apache.dolphinscheduler.api.service.AccessTokenService; |
||||||
|
import org.apache.dolphinscheduler.api.utils.Result; |
||||||
|
import org.apache.dolphinscheduler.common.Constants; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.User; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.http.HttpStatus; |
||||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||||
|
import org.springframework.web.bind.annotation.RequestAttribute; |
||||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus; |
||||||
|
import org.springframework.web.bind.annotation.RestController; |
||||||
|
|
||||||
|
import springfox.documentation.annotations.ApiIgnore; |
||||||
|
|
||||||
|
import static org.apache.dolphinscheduler.api.enums.Status.CREATE_ACCESS_TOKEN_ERROR; |
||||||
|
|
||||||
|
/** |
||||||
|
* access token controller |
||||||
|
*/ |
||||||
|
@Api(tags = "ACCESS_TOKEN_TAG") |
||||||
|
@RestController |
||||||
|
@RequestMapping("/v2/access-tokens") |
||||||
|
public class AccessTokenV2Controller extends BaseController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private AccessTokenService accessTokenService; |
||||||
|
|
||||||
|
/** |
||||||
|
* create token |
||||||
|
* |
||||||
|
* @param loginUser login user |
||||||
|
* @param createTokenRequest createTokenRequest |
||||||
|
* @return CreateTokenResponse CreateTokenResponse |
||||||
|
*/ |
||||||
|
@ApiOperation(value = "createToken", notes = "CREATE_TOKEN_NOTES") |
||||||
|
@PostMapping(consumes = {"application/json"}) |
||||||
|
@ResponseStatus(HttpStatus.CREATED) |
||||||
|
@ApiException(CREATE_ACCESS_TOKEN_ERROR) |
||||||
|
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") |
||||||
|
public CreateTokenResponse createToken(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, |
||||||
|
@RequestBody CreateTokenRequest createTokenRequest) { |
||||||
|
Result result = accessTokenService.createToken(loginUser, |
||||||
|
createTokenRequest.getUserId(), |
||||||
|
createTokenRequest.getExpireTime(), |
||||||
|
createTokenRequest.getToken()); |
||||||
|
return new CreateTokenResponse(result); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,56 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.apache.dolphinscheduler.api.dto; |
||||||
|
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty; |
||||||
|
|
||||||
|
public class CreateTokenRequest { |
||||||
|
@ApiModelProperty(example = "1", required = true) |
||||||
|
Integer userId; |
||||||
|
|
||||||
|
@ApiModelProperty(example = "2022-12-31 00:00:00", required = true) |
||||||
|
String expireTime; |
||||||
|
|
||||||
|
@ApiModelProperty(example = "abc123xyz") |
||||||
|
String token; |
||||||
|
|
||||||
|
public Integer getUserId() { |
||||||
|
return userId; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUserId(Integer userId) { |
||||||
|
this.userId = userId; |
||||||
|
} |
||||||
|
|
||||||
|
public String getExpireTime() { |
||||||
|
return expireTime; |
||||||
|
} |
||||||
|
|
||||||
|
public void setExpireTime(String expireTime) { |
||||||
|
this.expireTime = expireTime; |
||||||
|
} |
||||||
|
|
||||||
|
public String getToken() { |
||||||
|
return token; |
||||||
|
} |
||||||
|
|
||||||
|
public void setToken(String token) { |
||||||
|
this.token = token; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,41 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.apache.dolphinscheduler.api.dto; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.api.utils.Result; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.AccessToken; |
||||||
|
|
||||||
|
public class CreateTokenResponse extends Result { |
||||||
|
private AccessToken data; |
||||||
|
|
||||||
|
public CreateTokenResponse(Result result) { |
||||||
|
super(); |
||||||
|
this.setCode(result.getCode()); |
||||||
|
this.setMsg(result.getMsg()); |
||||||
|
this.setData((AccessToken) result.getData()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public AccessToken getData() { |
||||||
|
return data; |
||||||
|
} |
||||||
|
|
||||||
|
public void setData(AccessToken data) { |
||||||
|
this.data = data; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
# |
||||||
|
# Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
# contributor license agreements. See the NOTICE file distributed with |
||||||
|
# this work for additional information regarding copyright ownership. |
||||||
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
# (the "License"); you may not use this file except in compliance with |
||||||
|
# the License. You may obtain a copy of the License at |
||||||
|
# |
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0 |
||||||
|
# |
||||||
|
# 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. |
||||||
|
# |
||||||
|
|
||||||
|
springfox.documentation.swagger.v2.path=/api-docs |
@ -0,0 +1,101 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.apache.dolphinscheduler.api.controller; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.api.enums.Status; |
||||||
|
import org.apache.dolphinscheduler.api.utils.Result; |
||||||
|
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Test; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.http.MediaType; |
||||||
|
import org.springframework.test.web.servlet.MvcResult; |
||||||
|
import org.springframework.util.LinkedMultiValueMap; |
||||||
|
import org.springframework.util.MultiValueMap; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||||
|
|
||||||
|
/** |
||||||
|
* access token v2 controller test |
||||||
|
*/ |
||||||
|
public class AccessTokenV2ControllerTest extends AbstractControllerTest { |
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AccessTokenV2ControllerTest.class); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testCreateToken() throws Exception { |
||||||
|
Map<String, Object> paramsMap = new HashMap<>(); |
||||||
|
paramsMap.put("userId", 1); |
||||||
|
paramsMap.put("expireTime", "2022-12-31 00:00:00"); |
||||||
|
paramsMap.put("token", "607f5aeaaa2093dbdff5d5522ce00510"); |
||||||
|
MvcResult mvcResult = mockMvc.perform(post("/v2/access-tokens") |
||||||
|
.header("sessionId", sessionId) |
||||||
|
.contentType(MediaType.APPLICATION_JSON) |
||||||
|
.content(JSONUtils.toJsonString(paramsMap))) |
||||||
|
.andExpect(status().isCreated()) |
||||||
|
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
||||||
|
.andReturn(); |
||||||
|
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); |
||||||
|
logger.info(mvcResult.getResponse().getContentAsString()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testCreateTokenIfAbsent() throws Exception { |
||||||
|
Map<String, Object> paramsMap = new HashMap<>(); |
||||||
|
paramsMap.put("userId", 1); |
||||||
|
paramsMap.put("expireTime", "2022-12-31 00:00:00"); |
||||||
|
paramsMap.put("token", null); |
||||||
|
|
||||||
|
MvcResult mvcResult = this.mockMvc |
||||||
|
.perform(post("/v2/access-tokens") |
||||||
|
.header("sessionId", this.sessionId) |
||||||
|
.contentType(MediaType.APPLICATION_JSON) |
||||||
|
.content(JSONUtils.toJsonString(paramsMap))) |
||||||
|
.andExpect(status().isCreated()) |
||||||
|
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
||||||
|
.andReturn(); |
||||||
|
|
||||||
|
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); |
||||||
|
logger.info(mvcResult.getResponse().getContentAsString()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testExceptionHandler() throws Exception { |
||||||
|
Map<String, Object> paramsMap = new HashMap<>(); |
||||||
|
paramsMap.put("userId", -1); |
||||||
|
paramsMap.put("expireTime", "2022-12-31 00:00:00"); |
||||||
|
paramsMap.put("token", "507f5aeaaa2093dbdff5d5522ce00510"); |
||||||
|
MvcResult mvcResult = mockMvc.perform(post("/v2/access-tokens") |
||||||
|
.header("sessionId", sessionId) |
||||||
|
.contentType(MediaType.APPLICATION_JSON) |
||||||
|
.content(JSONUtils.toJsonString(paramsMap))) |
||||||
|
.andExpect(status().isCreated()) |
||||||
|
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
||||||
|
.andReturn(); |
||||||
|
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); |
||||||
|
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), result.getCode().intValue()); |
||||||
|
logger.info(mvcResult.getResponse().getContentAsString()); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue