]> source.dussan.org Git - archiva.git/blob
41c8fcdee9dba3846fc53a9ef8c3ae6c3e910c0a
[archiva.git] /
1 package org.apache.archiva.redback.rest.services;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.redback.rest.api.model.Operation;
23 import org.apache.archiva.redback.rest.api.model.Permission;
24 import org.apache.archiva.redback.rest.api.model.User;
25 import org.apache.archiva.redback.rest.api.model.UserRegistrationRequest;
26 import org.apache.archiva.redback.rest.api.services.UserService;
27 import org.apache.archiva.redback.rest.services.mock.EmailMessage;
28 import org.apache.archiva.redback.rest.services.mock.ServicesAssert;
29 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
30 import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
31 import org.apache.cxf.jaxrs.client.WebClient;
32 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
33 import org.junit.Test;
34
35 import javax.ws.rs.core.MediaType;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.List;
39
40 import static org.fest.assertions.Assertions.assertThat;
41
42 /**
43  * @author Olivier Lamy
44  */
45 public class UserServiceTest
46     extends AbstractRestServicesTest
47 {
48
49
50     @Test
51     public void ping()
52         throws Exception
53     {
54         Boolean res = getUserService().ping();
55         assertTrue( res.booleanValue() );
56     }
57
58     @Test
59     public void getUsers()
60         throws Exception
61     {
62         UserService userService = getUserService();
63
64         WebClient.client( userService ).header( "Authorization", authorizationHeader );
65
66         List<User> users = userService.getUsers();
67         assertTrue( users != null );
68         assertFalse( users.isEmpty() );
69     }
70
71     @Test( expected = ServerWebApplicationException.class )
72     public void getUsersWithoutAuthz()
73         throws Exception
74     {
75         UserService userService = getUserService();
76         try
77         {
78             userService.getUsers();
79         }
80         catch ( ServerWebApplicationException e )
81         {
82             assertEquals( 403, e.getStatus() );
83             throw e;
84         }
85
86     }
87
88     @Test
89     public void getNoPermissionNotAuthz()
90         throws Exception
91     {
92
93         try
94         {
95             getFakeCreateAdminService().testAuthzWithoutKarmasNeededButAuthz();
96         }
97         catch ( ServerWebApplicationException e )
98         {
99             assertEquals( 403, e.getStatus() );
100         }
101     }
102
103     @Test
104     public void getNoPermissionAuthz()
105         throws Exception
106     {
107
108         try
109         {
110             FakeCreateAdminService service = getFakeCreateAdminService();
111
112             WebClient.client( service ).header( "Authorization", authorizationHeader );
113
114             assertTrue( service.testAuthzWithoutKarmasNeededButAuthz().booleanValue() );
115
116         }
117         catch ( ServerWebApplicationException e )
118         {
119             assertEquals( 403, e.getStatus() );
120         }
121     }
122
123     @Test
124     public void register()
125         throws Exception
126     {
127         try
128         {
129             UserService service = getUserService();
130             User u = new User();
131             u.setFullName( "the toto" );
132             u.setUsername( "toto" );
133             u.setEmail( "toto@toto.fr" );
134             u.setPassword( "toto123" );
135             u.setConfirmPassword( "toto123" );
136             String key = service.registerUser( new UserRegistrationRequest( u, "http://wine.fr/bordeaux" ) ).getKey();
137
138             assertFalse( key.equals( "-1" ) );
139
140             ServicesAssert assertService =
141                 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/testsService/",
142                                            ServicesAssert.class,
143                                            Collections.singletonList( new JacksonJaxbJsonProvider() ) );
144
145             List<EmailMessage> emailMessages = assertService.getEmailMessageSended();
146             assertEquals( 1, emailMessages.size() );
147             assertEquals( "toto@toto.fr", emailMessages.get( 0 ).getTos().get( 0 ) );
148
149             assertEquals( "Welcome", emailMessages.get( 0 ).getSubject() );
150             String messageContent = emailMessages.get( 0 ).getText();
151
152             log.info( "messageContent: {}", messageContent );
153
154             assertThat( messageContent ).contains( "Use the following URL to validate your account." ).contains(
155                 "http://wine.fr/bordeaux" ).containsIgnoringCase( "toto" );
156
157             assertTrue( service.validateUserFromKey( key ) );
158
159             service = getUserService( authorizationHeader );
160
161             u = service.getUser( "toto" );
162
163             assertNotNull( u );
164             assertTrue( u.isValidated() );
165             assertTrue( u.isPasswordChangeRequired() );
166
167             assertTrue( service.validateUserFromKey( key ) );
168
169         }
170         catch ( Exception e )
171         {
172             log.error( e.getMessage(), e );
173             throw e;
174         }
175         finally
176         {
177             getUserService( authorizationHeader ).deleteUser( "toto" );
178         }
179
180     }
181
182     @Test
183     public void registerNoUrl()
184         throws Exception
185     {
186         try
187         {
188             UserService service = getUserService();
189             User u = new User();
190             u.setFullName( "the toto" );
191             u.setUsername( "toto" );
192             u.setEmail( "toto@toto.fr" );
193             u.setPassword( "toto123" );
194             u.setConfirmPassword( "toto123" );
195             String key = service.registerUser( new UserRegistrationRequest( u, null ) ).getKey();
196
197             assertFalse( key.equals( "-1" ) );
198
199             ServicesAssert assertService =
200                 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/testsService/",
201                                            ServicesAssert.class,
202                                            Collections.singletonList( new JacksonJaxbJsonProvider() ) );
203
204             List<EmailMessage> emailMessages = assertService.getEmailMessageSended();
205             assertEquals( 1, emailMessages.size() );
206             assertEquals( "toto@toto.fr", emailMessages.get( 0 ).getTos().get( 0 ) );
207
208             assertEquals( "Welcome", emailMessages.get( 0 ).getSubject() );
209             String messageContent = emailMessages.get( 0 ).getText();
210
211             log.info( "messageContent: {}", messageContent );
212
213             assertThat( messageContent ).contains( "Use the following URL to validate your account." ).contains(
214                 "http://localhost:" + port ).containsIgnoringCase( "toto" );
215
216             assertTrue( service.validateUserFromKey( key ) );
217
218             service = getUserService( authorizationHeader );
219
220             u = service.getUser( "toto" );
221
222             assertNotNull( u );
223             assertTrue( u.isValidated() );
224             assertTrue( u.isPasswordChangeRequired() );
225
226             assertTrue( service.validateUserFromKey( key ) );
227
228         }
229         catch ( Exception e )
230         {
231             log.error( e.getMessage(), e );
232             throw e;
233         }
234         finally
235         {
236             getUserService( authorizationHeader ).deleteUser( "toto" );
237         }
238
239     }
240
241     @Test
242     public void resetPassword()
243         throws Exception
244     {
245         try
246         {
247             UserService service = getUserService();
248             User u = new User();
249             u.setFullName( "the toto" );
250             u.setUsername( "toto" );
251             u.setEmail( "toto@toto.fr" );
252             u.setPassword( "toto123" );
253             u.setConfirmPassword( "toto123" );
254             String key = service.registerUser( new UserRegistrationRequest( u, "http://wine.fr/bordeaux" ) ).getKey();
255
256             assertFalse( key.equals( "-1" ) );
257
258             ServicesAssert assertService =
259                 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/testsService/",
260                                            ServicesAssert.class,
261                                            Collections.singletonList( new JacksonJaxbJsonProvider() ) );
262
263             WebClient.client( assertService ).accept( MediaType.APPLICATION_JSON_TYPE );
264             WebClient.client( assertService ).type( MediaType.APPLICATION_JSON_TYPE );
265
266             List<EmailMessage> emailMessages = assertService.getEmailMessageSended();
267             assertEquals( 1, emailMessages.size() );
268             assertEquals( "toto@toto.fr", emailMessages.get( 0 ).getTos().get( 0 ) );
269
270             assertEquals( "Welcome", emailMessages.get( 0 ).getSubject() );
271             assertTrue(
272                 emailMessages.get( 0 ).getText().contains( "Use the following URL to validate your account." ) );
273
274             assertTrue( service.validateUserFromKey( key ) );
275
276             service = getUserService( authorizationHeader );
277
278             u = service.getUser( "toto" );
279
280             assertNotNull( u );
281             assertTrue( u.isValidated() );
282             assertTrue( u.isPasswordChangeRequired() );
283
284             assertTrue( service.validateUserFromKey( key ) );
285
286             assertTrue( service.resetPassword( "toto" ) );
287
288             emailMessages = assertService.getEmailMessageSended();
289             assertEquals( 2, emailMessages.size() );
290             assertEquals( "toto@toto.fr", emailMessages.get( 1 ).getTos().get( 0 ) );
291
292             assertTrue( emailMessages.get( 1 ).getText().contains( "Password Reset" ) );
293             assertTrue( emailMessages.get( 1 ).getText().contains( "Username: toto" ) );
294
295
296         }
297         catch ( Exception e )
298         {
299             log.error( e.getMessage(), e );
300             throw e;
301         }
302         finally
303         {
304             getUserService( authorizationHeader ).deleteUser( "toto" );
305         }
306
307     }
308
309     @Test
310     public void getAdminPermissions()
311         throws Exception
312     {
313         Collection<Permission> permissions = getUserService( authorizationHeader ).getUserPermissions( "admin" );
314         log.info( "admin permisssions:" + permissions );
315     }
316
317     @Test
318     public void getGuestPermissions()
319         throws Exception
320     {
321         createGuestIfNeeded();
322         Collection<Permission> permissions = getUserService().getCurrentUserPermissions();
323         log.info( "guest permisssions:" + permissions );
324     }
325
326     @Test
327     public void getAdminOperations()
328         throws Exception
329     {
330         Collection<Operation> operations = getUserService( authorizationHeader ).getUserOperations( "admin" );
331         log.info( "admin operations:" + operations );
332     }
333
334     @Test
335     public void getGuestOperations()
336         throws Exception
337     {
338         createGuestIfNeeded();
339         Collection<Operation> operations = getUserService().getCurrentUserOperations();
340         log.info( "guest operations:" + operations );
341     }
342
343     @Test
344     public void updateMe()
345         throws Exception
346     {
347         User u = new User();
348         u.setFullName( "the toto" );
349         u.setUsername( "toto" );
350         u.setEmail( "toto@toto.fr" );
351         u.setPassword( "toto123" );
352         u.setConfirmPassword( "toto123" );
353         u.setValidated( true );
354         getUserService( authorizationHeader ).createUser( u );
355
356         u.setFullName( "the toto123" );
357         u.setEmail( "toto@titi.fr" );
358         u.setPassword( "toto1234" );
359         u.setPreviousPassword( "toto123" );
360         getUserService( encode( "toto", "toto123" ) ).updateMe( u );
361
362         u = getUserService( authorizationHeader ).getUser( "toto" );
363         assertEquals( "the toto123", u.getFullName() );
364         assertEquals( "toto@titi.fr", u.getEmail() );
365
366         u.setFullName( "the toto1234" );
367         u.setEmail( "toto@tititi.fr" );
368         u.setPassword( "toto12345" );
369         u.setPreviousPassword( "toto1234" );
370         getUserService( encode( "toto", "toto1234" ) ).updateMe( u );
371
372         u = getUserService( authorizationHeader ).getUser( "toto" );
373         assertEquals( "the toto1234", u.getFullName() );
374         assertEquals( "toto@tititi.fr", u.getEmail() );
375
376         getUserService( authorizationHeader ).deleteUser( "toto" );
377     }
378
379     public void guestUserCreate()
380         throws Exception
381     {
382         UserService userService = getUserService( authorizationHeader );
383         assertNull( userService.getGuestUser() );
384         assertNull( userService.createGuestUser() );
385
386     }
387
388     protected void createGuestIfNeeded()
389         throws Exception
390     {
391         UserService userService = getUserService( authorizationHeader );
392         if ( userService.getGuestUser() == null )
393         {
394             userService.createGuestUser();
395         }
396     }
397
398 }