虽然我的测试方法是用@WithMockUser注释的,但我仍然会获得Access Denied.为什么这不适用于集成测试?使用@WebAppConfiguration和MockMvc测试一切都很好.
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FileUploadIntegrationTest {
@Autowired
private TestRestTemplate restTemplate;
@MockBean
private FileStorageService storageService;
@Test
public void classPathResourceTest() throws Exception {
ClassPathResource resource = new ClassPathResource("/test/testFile.txt", getClass());
assertThat(resource.exists(), is(true));
}
@Test
@WithMockUser(username="tester",roles={"USER"})
public void shouldUploadFile() throws Exception {
ClassPathResource resource = new ClassPathResource("/test/testFile.txt", getClass());
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("file", resource);
ResponseEntity<String> response = this.restTemplate.postForEntity("/files", map, String.class);
// assertThat(response.getStatusCode(), is(HttpStatus.OK));
then(storageService).should().addFile((any(String.class)), any(MultipartFile.class));
}
}
Run Code Online (Sandbox Code Playgroud)
控制器类:
@RestController
@RequestMapping("/files")
@PreAuthorize(value = "hasRole('ROLE_USER')")
public class FileUploadController …Run Code Online (Sandbox Code Playgroud) 我有一个问题,就是SpEL在此存储库的第二种方法中将实体参数评估为空引用。第一种方法可以正常工作,并且id应该正确地评估为Long。
@NoRepositoryBean
public interface SecuredPagingAndSortingRepository<T extends AuditedEntity, ID extends Serializable>
extends PagingAndSortingRepository<T, ID> {
@Override
@RestResource(exported = false)
@PreAuthorize("hasPermission(#id, null, 'owner')")
void delete(ID id);
@Override
@PreAuthorize("hasPermission(#entity, 'owner')")
void delete(T entity);
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义PermissionEvaluator:
@Slf4j
@Component
public class CustomPermissionEvaluator implements PermissionEvaluator {
private final PermissionResolverFactory permissionResolverFactory;
@Autowired
public CustomPermissionEvaluator(PermissionResolverFactory permissionResolverFactory) {
this.permissionResolverFactory = permissionResolverFactory;
}
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
Assert.notNull(userDetails, "User details cannot be null");
Assert.notNull(targetDomainObject, "Target object cannot be null"); …Run Code Online (Sandbox Code Playgroud) 我无法在WebStorm 2016.3.2或IntelliJ IDEA 2016.3.2的NPM运行/调试配置中看到自定义NPM脚本.我选择了正确的package.json文件.
这是我的package.json文件的脚本部分和下面屏幕上的运行/调试配置.我不能选择其他的自定义命令,如clean:dist(只有clean)e2e,pretest,test:passive等我怎样才能选择这些?
"scripts": {
"clean": "npm cache clean && rimraf coverage src/main/resources/static/*",
"clean:dist": "rimraf typings src/main/resources/static/*",
"preclean:install": "npm run clean",
"clean:install": "npm set progress=false && npm install",
"preclean:start": "npm run clean",
"clean:start": "npm start",
"pree2e": "webdriver-manager update",
"e2e": "protractor",
"e2e:live": "protractor --elementExplorer",
"lint": "tslint './src/main/frontend/**/*.ts' --force",
"pretest": "rimraf coverage && npm run lint",
"test:passive": "ng test -w false",
"test": "ng test",
"pretest:phantom": "rimraf coverage && npm run …Run Code Online (Sandbox Code Playgroud)