Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPMRPP-95844 get invitation by #2135

Open
wants to merge 2 commits into
base: feature/orgs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dependencies {
} else {
implementation 'com.github.reportportal:commons-dao:b1599ca'
implementation 'com.github.reportportal:commons:2b470a3'
implementation 'com.github.reportportal:plugin-api:c2a742a'
implementation 'com.github.reportportal:plugin-api:d96f33f'
}

implementation 'org.springframework.boot:spring-boot-starter-aop'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.epam.ta.reportportal.entity.jasper.ReportFormat;
import com.epam.ta.reportportal.entity.organization.MembershipDetails;
import com.epam.ta.reportportal.model.YesNoRS;
import com.epam.ta.reportportal.model.user.UserBidRS;
import com.epam.ta.reportportal.model.user.UserResource;
import java.io.OutputStream;
import java.util.Map;
Expand Down Expand Up @@ -62,14 +61,6 @@ public interface GetUserHandler {
InstanceUser getCurrentUser(ReportPortalUser currentUser);


/**
* Get information about user registration bid
*
* @param uuid UUID
* @return {@link UserBidRS}
*/
UserBidRS getBidInformation(String uuid);

/**
* Validate existence of username or email
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public interface UserInvitationHandler {
Invitation createUserInvitation(InvitationRequest request, ReportPortalUser username,
String userRegURL);

/**
* Retrieve an invitation by its ID.
*
* @param invitationId the ID of the invitation to retrieve
* @param baseUrl the base url of the webservice
* @return the invitation corresponding to the given ID
*/
Invitation getInvitation(String invitationId, String baseUrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.epam.ta.reportportal.commons.querygen.constant.UserCriteriaConstant.CRITERIA_EMAIL;
import static com.epam.ta.reportportal.commons.querygen.constant.UserCriteriaConstant.CRITERIA_EXPIRED;
import static com.epam.ta.reportportal.commons.querygen.constant.UserCriteriaConstant.CRITERIA_USER;
import static com.epam.ta.reportportal.core.user.impl.CreateUserHandlerImpl.INTERNAL_BID_TYPE;
import static com.epam.ta.reportportal.util.OffsetUtils.responseWithPageParameters;
import static com.epam.ta.reportportal.ws.converter.converters.UserConverter.TO_INSTANCE_USER;
import static java.util.Optional.ofNullable;
Expand All @@ -48,9 +47,7 @@
import com.epam.ta.reportportal.entity.project.ProjectUtils;
import com.epam.ta.reportportal.entity.user.ProjectUser;
import com.epam.ta.reportportal.entity.user.User;
import com.epam.ta.reportportal.entity.user.UserCreationBid;
import com.epam.ta.reportportal.model.YesNoRS;
import com.epam.ta.reportportal.model.user.UserBidRS;
import com.epam.ta.reportportal.model.user.UserResource;
import com.epam.ta.reportportal.util.PersonalProjectService;
import com.epam.ta.reportportal.ws.converter.PagedResourcesAssembler;
Expand Down Expand Up @@ -146,22 +143,6 @@ public Iterable<UserResource> getUsers(Filter filter, Pageable pageable,
.apply(userRepository.findByFilterExcluding(filter, pageable, "email"));
}

@Override
public UserBidRS getBidInformation(String uuid) {
Optional<UserCreationBid> bid = userCreationBidRepository.findByUuidAndType(uuid,
INTERNAL_BID_TYPE);
return bid.map(b -> {
UserBidRS rs = new UserBidRS();
rs.setIsActive(true);
rs.setEmail(b.getEmail());
rs.setUuid(b.getUuid());
return rs;
}).orElseGet(() -> {
UserBidRS rs = new UserBidRS();
rs.setIsActive(false);
return rs;
});
}

@Override
public YesNoRS validateInfo(String username, String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.epam.ta.reportportal.entity.user.UserCreationBid;
import com.epam.ta.reportportal.util.UserUtils;
import com.epam.ta.reportportal.util.email.MailServiceFactory;
import com.epam.ta.reportportal.ws.converter.converters.InvitationConverter;
import com.google.common.collect.Maps;
import java.net.URI;
import java.time.Instant;
Expand Down Expand Up @@ -116,22 +117,22 @@ public Invitation createUserInvitation(InvitationRequest request, ReportPortalUs
userBid.setInvitingUser(user);
userBid.setMetadata(getUserCreationBidMetadata(request.getOrganizations()));

UserCreationBid storedUserBid;
try {
userCreationBidRepository.save(userBid);
storedUserBid = userCreationBidRepository.save(userBid);
} catch (Exception e) {
throw new ReportPortalException("Error while user creation bid registering.", e);
}

StringBuilder emailLink = new StringBuilder(baseUrl)
.append("/ui/#registration?uuid=")
.append(userBid.getUuid());

var response = new Invitation();
response.setCreatedAt(now);
response.setExpiresAt(now.plus(1, ChronoUnit.DAYS));
response.setCreatedAt(storedUserBid.getLastModified());
response.setExpiresAt(storedUserBid.getLastModified().plus(1, ChronoUnit.DAYS));
response.setId(UUID.fromString(userBid.getUuid()));
response.setLink(URI.create(emailLink.toString()));
response.setLink(getEmailLink(baseUrl, userBid.getUuid()));
response.setEmail(request.getEmail());
response.setStatus(PENDING);
response.setUserId(user.getId());
response.setFullName(user.getFullName());

/*
emailExecutorService.execute(() -> emailServiceFactory.getEmailService(integration, false)
Expand All @@ -145,6 +146,14 @@ public Invitation createUserInvitation(InvitationRequest request, ReportPortalUs
return response;
}

@Override
public Invitation getInvitation(String invitationId, String baseUrl) {
var bid = userCreationBidRepository.getById(invitationId);
var invitation = InvitationConverter.TO_INVITATION.apply(bid);
invitation.setLink(getEmailLink(baseUrl, bid.getUuid()));
return invitation;
}

private void validateInvitationRequest(InvitationRequest request) {
expect(UserUtils.isEmailValid(request.getEmail().trim()), equalTo(true))
.verify(BAD_REQUEST_ERROR, formattedSupplier("email='{}'", request.getEmail()));
Expand Down Expand Up @@ -193,4 +202,8 @@ private boolean isSsoEnabled() {
return settingsRepository.findByKey(SERVER_USERS_SSO).map(ServerSettings::getValue)
.map(Boolean::parseBoolean).orElse(false);
}

private URI getEmailLink(String baseUrl, String invitationId) {
return URI.create(baseUrl + "/ui/#registration?uuid=" + invitationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

import static com.epam.ta.reportportal.auth.permissions.Permissions.INVITATION_ALLOWED;
import static com.epam.ta.reportportal.core.launch.util.LinkGenerator.composeBaseUrl;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

import com.epam.reportportal.api.InvitationApi;
import com.epam.reportportal.api.model.Invitation;
import com.epam.reportportal.api.model.InvitationRequest;
import com.epam.ta.reportportal.core.user.UserInvitationHandler;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -54,8 +55,18 @@ public ResponseEntity<Invitation> postInvitations(InvitationRequest invitationRe
composeBaseUrl(httpServletRequest));

return ResponseEntity
.status(OK)
.status(CREATED)
.body(response);
}


@Override
public ResponseEntity<Invitation> getInvitationsId(String invitationId) {
return ResponseEntity
.status(OK)
.body(
userInvitationHandler.getInvitation(invitationId, composeBaseUrl(httpServletRequest)));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import com.epam.ta.reportportal.model.user.EditUserRQ;
import com.epam.ta.reportportal.model.user.ResetPasswordRQ;
import com.epam.ta.reportportal.model.user.RestorePasswordRQ;
import com.epam.ta.reportportal.model.user.UserBidRS;
import com.epam.ta.reportportal.model.user.UserResource;
import com.epam.ta.reportportal.ws.reporting.OperationCompletionRS;
import com.epam.ta.reportportal.ws.resolver.ActiveRole;
Expand Down Expand Up @@ -137,11 +136,6 @@ public CreateUserRS createUser(@RequestBody @Validated CreateUserRQConfirm reque
return createUserMessageHandler.createUser(request, uuid);
}

@Transactional(readOnly = true)
@GetMapping(value = "/registration")
public UserBidRS getUserBidInfo(@RequestParam(value = "uuid") String uuid) {
return getUserHandler.getBidInformation(uuid);
}

@DeleteMapping(value = "/{id}")
@Operation(summary = "Delete specified user")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.ta.reportportal.ws.converter.converters;

import com.epam.reportportal.api.model.Invitation;
import com.epam.reportportal.api.model.InvitationStatus;
import com.epam.ta.reportportal.entity.user.UserCreationBid;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
import java.util.function.Function;

public class InvitationConverter {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [reviewdog] <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck> reported by reviewdog 🐶
Missing a Javadoc comment.


private InvitationConverter() {
}

public static final Function<UserCreationBid, Invitation> TO_INVITATION = bid -> {
var invitation = new Invitation();

invitation.setId(UUID.fromString(bid.getUuid()));
invitation.setEmail(bid.getEmail());
invitation.setStatus(InvitationStatus.PENDING);
invitation.setCreatedAt(bid.getLastModified());
invitation.setExpiresAt(bid.getLastModified().plus(1, ChronoUnit.DAYS));
invitation.setUserId(bid.getInvitingUser().getId());
invitation.setFullName(bid.getInvitingUser().getFullName());

return invitation;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@
package com.epam.ta.reportportal.core.user.impl;

import static com.epam.ta.reportportal.ReportPortalUserUtil.getRpUser;
import static com.epam.ta.reportportal.core.user.impl.CreateUserHandlerImpl.INTERNAL_BID_TYPE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.dao.UserCreationBidRepository;
import com.epam.ta.reportportal.dao.UserRepository;
import com.epam.ta.reportportal.entity.organization.OrganizationRole;
import com.epam.ta.reportportal.entity.project.ProjectRole;
import com.epam.ta.reportportal.entity.user.User;
import com.epam.ta.reportportal.entity.user.UserRole;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.model.YesNoRS;
import com.epam.ta.reportportal.model.user.UserBidRS;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -78,18 +75,6 @@ void getNotExistedUserByLoggedInUser() {
assertEquals("User 'not_exist' not found.", exception.getMessage());
}

@Test
void getEmptyBidInfo() {
String uuid = "uuid";

when(userCreationBidRepository.findByUuidAndType(uuid, INTERNAL_BID_TYPE)).thenReturn(
Optional.empty());

UserBidRS bidInformation = handler.getBidInformation(uuid);
assertFalse(bidInformation.getIsActive());
assertNull(bidInformation.getEmail());
assertNull(bidInformation.getUuid());
}

@Test
void validateInfoByNotExistUsername() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.epam.reportportal.api.model.Invitation;
Expand All @@ -34,7 +36,6 @@
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

class InvitationControllerTest extends BaseMvcTest {

Expand Down Expand Up @@ -65,11 +66,11 @@ void createInvitationByAdmin() throws Exception {
rq.setEmail("[email protected]");
rq.setOrganizations(organizations);

var result = mockMvc.perform(MockMvcRequestBuilders.post(INVITATIONS_ENDPOINT)
var result = mockMvc.perform(post(INVITATIONS_ENDPOINT)
.content(objectMapper.writeValueAsBytes(rq))
.contentType(APPLICATION_JSON)
.with(token(oAuthHelper.getSuperadminToken())))
.andExpect(status().isOk())
.andExpect(status().isCreated())
.andReturn()
.getResponse().getContentAsString();

Expand All @@ -79,6 +80,17 @@ void createInvitationByAdmin() throws Exception {

assertEquals(InvitationStatus.PENDING, invitation.getStatus());

var storedInvitationString = mockMvc.perform(get(INVITATIONS_ENDPOINT + "/" + invitation.getId())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [reviewdog] <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck> reported by reviewdog 🐶
Line is longer than 100 characters (found 101).

.content(objectMapper.writeValueAsBytes(rq))
.contentType(APPLICATION_JSON)
.with(token(oAuthHelper.getSuperadminToken())))
.andExpect(status().isOk())
.andReturn()
.getResponse().getContentAsString();
var storedInvitation = objectMapper.readValue(storedInvitationString, Invitation.class);

assertEquals(storedInvitation, invitation);

}


Expand All @@ -104,7 +116,7 @@ void createInvitationNotEnoughPermissions() throws Exception {
rq.setEmail("[email protected]");
rq.setOrganizations(organizations);

mockMvc.perform(MockMvcRequestBuilders.post(INVITATIONS_ENDPOINT)
mockMvc.perform(post(INVITATIONS_ENDPOINT)
.content(objectMapper.writeValueAsBytes(rq))
.contentType(APPLICATION_JSON)
.with(token(oAuthHelper.getDefaultToken())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,6 @@ void createUserPositive() throws Exception {
assertTrue(userRepository.findById(createUserRS.getId()).isPresent());
}

@Test
void getUserBidInfoPositive() throws Exception {
mockMvc.perform(get(USERS_URL + "/registration?uuid=e5f98deb-8966-4b2d-ba2f-35bc69d30c06"))
.andExpect(status().isOk());
}

@Test
void deleteUserNegative() throws Exception {
Expand Down
Loading