1 package org.apache.archiva.redback.rest.services;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
35 import javax.ws.rs.core.MediaType;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.List;
40 import static org.fest.assertions.Assertions.assertThat;
43 * @author Olivier Lamy
45 public class UserServiceTest
46 extends AbstractRestServicesTest
54 Boolean res = getUserService().ping();
55 assertTrue( res.booleanValue() );
59 public void getUsers()
62 UserService userService = getUserService();
64 WebClient.client( userService ).header( "Authorization", authorizationHeader );
66 List<User> users = userService.getUsers();
67 assertTrue( users != null );
68 assertFalse( users.isEmpty() );
71 @Test( expected = ServerWebApplicationException.class )
72 public void getUsersWithoutAuthz()
75 UserService userService = getUserService();
78 userService.getUsers();
80 catch ( ServerWebApplicationException e )
82 assertEquals( 403, e.getStatus() );
89 public void getNoPermissionNotAuthz()
95 getFakeCreateAdminService().testAuthzWithoutKarmasNeededButAuthz();
97 catch ( ServerWebApplicationException e )
99 assertEquals( 403, e.getStatus() );
104 public void getNoPermissionAuthz()
110 FakeCreateAdminService service = getFakeCreateAdminService();
112 WebClient.client( service ).header( "Authorization", authorizationHeader );
114 assertTrue( service.testAuthzWithoutKarmasNeededButAuthz().booleanValue() );
117 catch ( ServerWebApplicationException e )
119 assertEquals( 403, e.getStatus() );
124 public void register()
129 UserService service = getUserService();
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();
138 assertFalse( key.equals( "-1" ) );
140 ServicesAssert assertService =
141 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/testsService/",
142 ServicesAssert.class,
143 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
145 List<EmailMessage> emailMessages = assertService.getEmailMessageSended();
146 assertEquals( 1, emailMessages.size() );
147 assertEquals( "toto@toto.fr", emailMessages.get( 0 ).getTos().get( 0 ) );
149 assertEquals( "Welcome", emailMessages.get( 0 ).getSubject() );
150 String messageContent = emailMessages.get( 0 ).getText();
152 log.info( "messageContent: {}", messageContent );
154 assertThat( messageContent ).contains( "Use the following URL to validate your account." ).contains(
155 "http://wine.fr/bordeaux" ).containsIgnoringCase( "toto" );
157 assertTrue( service.validateUserFromKey( key ) );
159 service = getUserService( authorizationHeader );
161 u = service.getUser( "toto" );
164 assertTrue( u.isValidated() );
165 assertTrue( u.isPasswordChangeRequired() );
167 assertTrue( service.validateUserFromKey( key ) );
170 catch ( Exception e )
172 log.error( e.getMessage(), e );
177 getUserService( authorizationHeader ).deleteUser( "toto" );
183 public void registerNoUrl()
188 UserService service = getUserService();
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();
197 assertFalse( key.equals( "-1" ) );
199 ServicesAssert assertService =
200 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/testsService/",
201 ServicesAssert.class,
202 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
204 List<EmailMessage> emailMessages = assertService.getEmailMessageSended();
205 assertEquals( 1, emailMessages.size() );
206 assertEquals( "toto@toto.fr", emailMessages.get( 0 ).getTos().get( 0 ) );
208 assertEquals( "Welcome", emailMessages.get( 0 ).getSubject() );
209 String messageContent = emailMessages.get( 0 ).getText();
211 log.info( "messageContent: {}", messageContent );
213 assertThat( messageContent ).contains( "Use the following URL to validate your account." ).contains(
214 "http://localhost:" + port ).containsIgnoringCase( "toto" );
216 assertTrue( service.validateUserFromKey( key ) );
218 service = getUserService( authorizationHeader );
220 u = service.getUser( "toto" );
223 assertTrue( u.isValidated() );
224 assertTrue( u.isPasswordChangeRequired() );
226 assertTrue( service.validateUserFromKey( key ) );
229 catch ( Exception e )
231 log.error( e.getMessage(), e );
236 getUserService( authorizationHeader ).deleteUser( "toto" );
242 public void resetPassword()
247 UserService service = getUserService();
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();
256 assertFalse( key.equals( "-1" ) );
258 ServicesAssert assertService =
259 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/testsService/",
260 ServicesAssert.class,
261 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
263 WebClient.client( assertService ).accept( MediaType.APPLICATION_JSON_TYPE );
264 WebClient.client( assertService ).type( MediaType.APPLICATION_JSON_TYPE );
266 List<EmailMessage> emailMessages = assertService.getEmailMessageSended();
267 assertEquals( 1, emailMessages.size() );
268 assertEquals( "toto@toto.fr", emailMessages.get( 0 ).getTos().get( 0 ) );
270 assertEquals( "Welcome", emailMessages.get( 0 ).getSubject() );
272 emailMessages.get( 0 ).getText().contains( "Use the following URL to validate your account." ) );
274 assertTrue( service.validateUserFromKey( key ) );
276 service = getUserService( authorizationHeader );
278 u = service.getUser( "toto" );
281 assertTrue( u.isValidated() );
282 assertTrue( u.isPasswordChangeRequired() );
284 assertTrue( service.validateUserFromKey( key ) );
286 assertTrue( service.resetPassword( "toto" ) );
288 emailMessages = assertService.getEmailMessageSended();
289 assertEquals( 2, emailMessages.size() );
290 assertEquals( "toto@toto.fr", emailMessages.get( 1 ).getTos().get( 0 ) );
292 assertTrue( emailMessages.get( 1 ).getText().contains( "Password Reset" ) );
293 assertTrue( emailMessages.get( 1 ).getText().contains( "Username: toto" ) );
297 catch ( Exception e )
299 log.error( e.getMessage(), e );
304 getUserService( authorizationHeader ).deleteUser( "toto" );
310 public void getAdminPermissions()
313 Collection<Permission> permissions = getUserService( authorizationHeader ).getUserPermissions( "admin" );
314 log.info( "admin permisssions:" + permissions );
318 public void getGuestPermissions()
321 createGuestIfNeeded();
322 Collection<Permission> permissions = getUserService().getCurrentUserPermissions();
323 log.info( "guest permisssions:" + permissions );
327 public void getAdminOperations()
330 Collection<Operation> operations = getUserService( authorizationHeader ).getUserOperations( "admin" );
331 log.info( "admin operations:" + operations );
335 public void getGuestOperations()
338 createGuestIfNeeded();
339 Collection<Operation> operations = getUserService().getCurrentUserOperations();
340 log.info( "guest operations:" + operations );
344 public void updateMe()
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 );
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 );
362 u = getUserService( authorizationHeader ).getUser( "toto" );
363 assertEquals( "the toto123", u.getFullName() );
364 assertEquals( "toto@titi.fr", u.getEmail() );
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 );
372 u = getUserService( authorizationHeader ).getUser( "toto" );
373 assertEquals( "the toto1234", u.getFullName() );
374 assertEquals( "toto@tititi.fr", u.getEmail() );
376 getUserService( authorizationHeader ).deleteUser( "toto" );
379 public void guestUserCreate()
382 UserService userService = getUserService( authorizationHeader );
383 assertNull( userService.getGuestUser() );
384 assertNull( userService.createGuestUser() );
388 protected void createGuestIfNeeded()
391 UserService userService = getUserService( authorizationHeader );
392 if ( userService.getGuestUser() == null )
394 userService.createGuestUser();