java - Integration tests with spring security -
i need send request api, despite having placed administrator annotation error @withmockuser(roles="administrador")
.
how send request?
api
@requestmapping(value = "/{id}", method = requestmethod.get) @postauthorize("returnobject.instancia == principal.instancia.instancia") public validacao retrieve(@pathvariable("id") string id) { return validacaoservice.retrieve(id); }
test
@test @withmockuser(roles = "administrador") public void testcretrieve() throws exception { this.mockmvc .perform(get("/api/validacao/" + id).with(user("daniela.morais@sofist.com.br"))) .andexpect(status().isok()) .andreturn(); }
log
org.springframework.web.util.nestedservletexception: request processing failed; nested exception org.springframework.security.authentication.authenticationcredentialsnotfoundexception: authentication object not found in securitycontext
test class
@fixmethodorder(methodsorters.name_ascending) @runwith(springjunit4classrunner.class) @contextconfiguration(classes = {validacaoapitest.testconfiguration.class, withsecurityconfig.class}) @webappconfiguration public class validacaoapitest { @enablewebmvc @configuration public static class testconfiguration { fongo fongo = new fongo("new server 1"); db db = fongo.getdb("oknok"); @bean validacaoapi getvalidacaoapi() { return new validacaoapi(); } @bean activeuser getactiveuser() { activeuser mock = mockito.mock(activeuser.class); when(mock.getuser()).thenreturn(new user().setemail("email@email.com")); when(mock.getinstancia()).thenreturn(new instancia().setinstancia("instancia")); return mock; } @bean validacaoservice getvalidacaoservice() { return new validacaoservice(); } @bean matchservice getmatchservice() { return new matchservice(); } @bean planilhareader getplanilhareader() { return new planilhareader(); } @bean atributoreader getatributoreader() { return new atributoreader(); } @bean atributodao getatributodao() { return new atributodao(); } @bean uploadservice getuploadservice() { return new uploadservice(); } @bean validacaoresultadodao getvalidacaoresultadodao() { return new validacaoresultadodao(db); } @bean mapper getmapper() { return new mapper(db); } @bean uploaddao getuploaddao() { return new uploaddao(db); } @bean matchdao getmatchdao() { return new matchdao(db); } @bean validacaodao getvalidacaodao() { return new validacaodao(db); } @bean uploadoriginalsdao getuploadoriginalsdao() { return new uploadoriginalsdao(db); } @bean atributovalidator getatributovalidator() { return new atributovalidator(); } } @autowired matchservice matchservice; @autowired private webapplicationcontext context; private mockmvc mockmvc; private static string id; @before public void setup() { mockmvc = mockmvcbuilders.webappcontextsetup(context).build(); } @test public void testacreatevalidation() throws exception { mvcresult result = this.mockmvc .perform(post("/api/validacao")) .andexpect(status().isok()) .andexpect(jsonpath("$.id", notnullvalue())) .andreturn(); this.id = ((basicdbobject) json.parse(result.getresponse().getcontentasstring())).getstring("id"); } @test public void testbretrieveall() throws exception { mvcresult result = this.mockmvc .perform(get("/api/validacao")) .andexpect(status().isok()) .andexpect(jsonpath("$.[0].id", notnullvalue())) .andreturn(); basicdblist list = (basicdblist) json.parse(result.getresponse().getcontentasstring()); this.id = (string) ((basicdbobject) json.parse(list.get(0).tostring())).get("id"); } //fixme @test @withmockuser(roles = "administrador") public void testcretrieve() throws exception { this.mockmvc .perform(get("/api/validacao/" + id).with(user("daniela.morais@sofist.com.br"))) .andexpect(status().isok()) .andreturn(); } }
in spring security reference, section 10.1 states in order able test spring security features, need integrate security filter chain in mockmvc object, shown in example in @before setup method.
import static org.springframework.security.test.web.servlet.setup.securitymockmvcconfigurers.*; @runwith(springjunit4classrunner.class) @contextconfiguration @webappconfiguration public class csrfshowcasetests { @autowired private webapplicationcontext context; private mockmvc mvc; @before public void setup() { mvc = mockmvcbuilders .webappcontextsetup(context) .apply(springsecurity()) .build(); } ... }
Comments
Post a Comment