11package org .javaee7 .jaxrs .security .declarative ;
22
3+ import static com .gargoylesoftware .htmlunit .HttpMethod .POST ;
4+ import static com .gargoylesoftware .htmlunit .HttpMethod .PUT ;
5+ import static com .gargoylesoftware .htmlunit .util .UrlUtils .toUrlUnsafe ;
6+ import static org .javaee7 .ServerOperations .addUsersToContainerIdentityStore ;
37import static org .junit .Assert .assertEquals ;
4- import static org .junit .Assert .assertNotNull ;
58import static org .junit .Assert .assertTrue ;
69import static org .junit .Assert .fail ;
710
8- import java .io .ByteArrayInputStream ;
911import java .io .File ;
1012import java .io .IOException ;
1113import java .net .URL ;
12- import java .nio .file .Paths ;
13- import java .util .ArrayList ;
14- import java .util .List ;
1514
16- import org .javaee7 .CliCommands ;
1715import org .jboss .arquillian .container .test .api .Deployment ;
1816import org .jboss .arquillian .junit .Arquillian ;
1917import org .jboss .arquillian .test .api .ArquillianResource ;
2018import org .jboss .shrinkwrap .api .ShrinkWrap ;
2119import org .jboss .shrinkwrap .api .spec .WebArchive ;
20+ import org .junit .After ;
21+ import org .junit .Before ;
2222import org .junit .Test ;
2323import org .junit .runner .RunWith ;
2424import org .xml .sax .SAXException ;
2525
26- import com .meterware .httpunit .AuthorizationRequiredException ;
27- import com .meterware .httpunit .GetMethodWebRequest ;
28- import com .meterware .httpunit .HttpException ;
29- import com .meterware .httpunit .PostMethodWebRequest ;
30- import com .meterware .httpunit .PutMethodWebRequest ;
31- import com .meterware .httpunit .WebConversation ;
32- import com .meterware .httpunit .WebResponse ;
26+ import com .gargoylesoftware .htmlunit .DefaultCredentialsProvider ;
27+ import com .gargoylesoftware .htmlunit .FailingHttpStatusCodeException ;
28+ import com .gargoylesoftware .htmlunit .TextPage ;
29+ import com .gargoylesoftware .htmlunit .WebClient ;
30+ import com .gargoylesoftware .htmlunit .WebRequest ;
3331
3432/**
3533 * @author Arun Gupta
@@ -41,115 +39,94 @@ public class MyResourceTest {
4139 private URL base ;
4240
4341 private static final String WEBAPP_SRC = "src/main/webapp" ;
42+
43+ private WebClient webClient ;
44+ private DefaultCredentialsProvider correctCreds = new DefaultCredentialsProvider ();
45+ private DefaultCredentialsProvider incorrectCreds = new DefaultCredentialsProvider ();
46+
47+ @ Before
48+ public void setup () {
49+ webClient = new WebClient ();
50+ correctCreds .addCredentials ("u1" , "p1" );
51+ incorrectCreds .addCredentials ("random" , "random" );
52+ }
53+
54+ @ After
55+ public void tearDown () {
56+ webClient .closeAllWindows ();
57+ }
4458
4559 @ Deployment (testable = false )
4660 public static WebArchive createDeployment () {
4761
4862 addUsersToContainerIdentityStore ();
4963
5064 return ShrinkWrap .create (WebArchive .class )
51- .addAsWebInfResource ((new File (WEBAPP_SRC + "/WEB-INF" , "web.xml" )))
52- .addClasses (MyApplication .class , MyResource .class );
65+ .addAsWebInfResource ((new File (WEBAPP_SRC + "/WEB-INF" , "web.xml" )))
66+ .addClasses (MyApplication .class , MyResource .class );
5367 }
5468
5569 @ Test
5670 public void testGetWithCorrectCredentials () throws IOException , SAXException {
57- WebConversation conv = new WebConversation ();
58- conv .setAuthentication ("file" , "u1" , "p1" );
59- GetMethodWebRequest getRequest = new GetMethodWebRequest (base + "/webresources/myresource" );
60- WebResponse response = null ;
61- try {
62- response = conv .getResponse (getRequest );
63- } catch (AuthorizationRequiredException e ) {
64- fail (e .getMessage ());
65- }
66- assertNotNull (response );
67- assertTrue (response .getText ().contains ("get" ));
71+ webClient .setCredentialsProvider (correctCreds );
72+ TextPage page = webClient .getPage (base + "webresources/myresource" );
73+
74+ assertTrue (page .getContent () .contains ("get" ));
6875 }
6976
7077 @ Test
7178 public void testGetSubResourceWithCorrectCredentials () throws IOException , SAXException {
72- WebConversation conv = new WebConversation ();
73- conv .setAuthentication ("file" , "u1" , "p1" );
74- GetMethodWebRequest getRequest = new GetMethodWebRequest (base + "/webresources/myresource/1" );
75- WebResponse response = null ;
76- try {
77- response = conv .getResponse (getRequest );
78- } catch (AuthorizationRequiredException e ) {
79- fail (e .getMessage ());
80- }
81- assertNotNull (response );
79+ webClient .setCredentialsProvider (correctCreds );
80+ TextPage page = webClient .getPage (base + "webresources/myresource/1" );
8281
83- assertTrue (response . getText () .contains ("get1" ));
82+ assertTrue (page . getContent () .contains ("get1" ));
8483 }
8584
8685 @ Test
8786 public void testGetWithIncorrectCredentials () throws IOException , SAXException {
88- WebConversation conv = new WebConversation ();
89- conv .setAuthentication ("file" , "random" , "random" );
90- GetMethodWebRequest getRequest = new GetMethodWebRequest (base + "/webresources/myresource" );
87+ webClient .setCredentialsProvider (incorrectCreds );
88+
9189 try {
92- WebResponse response = conv . getResponse ( getRequest );
93- } catch (AuthorizationRequiredException e ) {
94- assertNotNull ( e );
90+ webClient . getPage ( base + "webresources/myresource" );
91+ } catch (FailingHttpStatusCodeException e ) {
92+ assertEquals ( 401 , e . getStatusCode () );
9593 return ;
9694 }
95+
9796 fail ("GET can be called with incorrect credentials" );
9897 }
9998
10099 @ Test
101100 public void testPost () throws IOException , SAXException {
102- WebConversation conv = new WebConversation ();
103- conv .setAuthentication ("file" , "u1" , "p1" );
104- PostMethodWebRequest postRequest = new PostMethodWebRequest (base + "/webresources/myresource" );
101+ webClient .setCredentialsProvider (correctCreds );
102+
105103 try {
106- WebResponse response = conv .getResponse (postRequest );
107- } catch (HttpException e ) {
108- assertNotNull (e );
109- assertEquals (403 , e .getResponseCode ());
104+ WebRequest postRequest = new WebRequest (toUrlUnsafe (base + "webresources/myresource" ), POST );
105+ postRequest .setRequestBody ("name=myname" );
106+ webClient .getPage (postRequest );
107+ } catch (FailingHttpStatusCodeException e ) {
108+ assertEquals (403 , e .getStatusCode ());
110109 return ;
111110 }
111+
112+ // All methods are excluded except for GET
112113 fail ("POST is not authorized and can still be called" );
113114 }
114115
115116 @ Test
116117 public void testPut () throws IOException , SAXException {
117- WebConversation conv = new WebConversation ();
118- conv .setAuthentication ("file" , "u1" , "p1" );
119- byte [] bytes = new byte [8 ];
120- ByteArrayInputStream bais = new ByteArrayInputStream (bytes );
121- PutMethodWebRequest putRequest = new PutMethodWebRequest (base + "/webresources/myresource" , bais , "text/plain" );
118+ webClient .setCredentialsProvider (correctCreds );
119+
122120 try {
123- WebResponse response = conv .getResponse (putRequest );
124- } catch (HttpException e ) {
125- assertNotNull (e );
126- assertEquals (403 , e .getResponseCode ());
121+ WebRequest postRequest = new WebRequest (toUrlUnsafe (base + "webresources/myresource" ), PUT );
122+ postRequest .setRequestBody ("name=myname" );
123+ webClient .getPage (postRequest );
124+ } catch (FailingHttpStatusCodeException e ) {
125+ assertEquals (403 , e .getStatusCode ());
127126 return ;
128127 }
129- fail ("PUT is not authorized and can still be called" );
130- }
131-
132- private static void addUsersToContainerIdentityStore () {
133-
134- // TODO: abstract adding container managed users to utility class
135- // TODO: consider PR for sending CLI commands to Arquillian
136-
137- String javaEEServer = System .getProperty ("javaEEServer" );
138128
139- if ("glassfish-remote" .equals (javaEEServer )) {
140- List <String > cmd = new ArrayList <>();
141-
142- cmd .add ("create-file-user" );
143- cmd .add ("--groups" );
144- cmd .add ("g1" );
145- cmd .add ("--passwordfile" );
146- cmd .add (Paths .get ("" ).toAbsolutePath () + "/src/test/resources/password.txt" );
147-
148- cmd .add ("u1" );
149-
150- CliCommands .payaraGlassFish (cmd );
151- }
152-
153- // TODO: support other servers than Payara and GlassFish
129+ // All methods are excluded except for GET
130+ fail ("PUT is not authorized and can still be called" );
154131 }
155132}
0 commit comments