Skip to content

Commit

Permalink
Feature parity auth/portalUser reached (#2)
Browse files Browse the repository at this point in the history
 - can create user
 - can get user
 - can update user
 - login slightly changed
  • Loading branch information
leni-kirilov authored Dec 31, 2016
1 parent e7b6ec2 commit 82f232c
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,137 @@
package com.cakecatalog.srv.auth.client;

import com.cakecatalog.srv.auth.client.model.CreatePortalUserRequest;
import com.cakecatalog.srv.auth.client.model.LoginRequest;
import com.cakecatalog.srv.auth.client.model.PortalUserResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

public class AuthClient {

private static final Logger log = LoggerFactory.getLogger(AuthClient.class);

private String url = "http://localhost:8008/";
public static final String LOGIN_URL = "/login";
public static final String PORTAL_USERS_URL = "/portalUsers";

private String url;
private RestTemplate restTemplate;

AuthClient() {}
AuthClient() {
this("http://localhost:8008/");
}

/**
* Pass the base URL to the AuthService
* @param url
*/
AuthClient(String url) {
this.url = url;
restTemplate = new RestTemplate();
}

//TODO need a client test
public boolean login(LoginRequest loginRequest) {
RestTemplate restTemplate = new RestTemplate();
Boolean isLoginSuccessful = restTemplate.postForObject(
url + "/login",
/**
* Checks user credentials
*
* @param loginRequest - email and pass
* @return logged user object or null if not succesful
*/
public PortalUserResponse login(LoginRequest loginRequest) {
log.info("Trying to log for user: " + loginRequest.getEmail());
PortalUserResponse loggedUser = restTemplate.postForObject(
url + LOGIN_URL,
loginRequest,
Boolean.class);
return isLoginSuccessful;
PortalUserResponse.class);
return loggedUser;
}

/**
* Create a new user
* @param name
* @param email
* @param password
* @return result object
*/
public PortalUserResponse createPortalUser(String name, String email, String password) {
log.info("Trying to create user: " + email);

PortalUserResponse createdUser = restTemplate.postForObject(
url + PORTAL_USERS_URL,
new CreatePortalUserRequest(name, email, password),
PortalUserResponse.class);

createdUser.setPassword(null);//hide password

return createdUser;
}

/**
* Update an existing user
* @param userId
* @param name
* @param password
*/
public void updatePortalUser(Long userId, String name, String password) {
log.info("Trying to updating existing user with id: " + userId);

PortalUserResponse portalUser = getPortalUser(userId);
portalUser.setName(name);
portalUser.setPassword(password);

//this updates the whole object, thus we get the whole user object first
restTemplate.put(
url + PORTAL_USERS_URL + "/" + userId,
portalUser);
}

/**
* Get a user object by its ID
* @param userId
* @return
*/
public PortalUserResponse getPortalUser(Long userId) {
log.info("Trying to get user with id: " + userId);

PortalUserResponse user = restTemplate.getForObject(
URI.create(url + PORTAL_USERS_URL + "/" + userId),
PortalUserResponse.class);

user.setPassword(null);//hide password

return user;
}

//TODO need a client test
public static void main(String args[]) {
boolean isSuccess = new AuthClient().login(new LoginRequest("[email protected]", "samplePassword"));
log.info("Login is successful?: " + isSuccess);
testUpdate();
testCreate();
testLogin();
}

static public class LoginRequest {
public String email;
public String password;
private static void testUpdate() {
log.info("Create + Updating + getting");
AuthClient authClient = new AuthClient();

PortalUserResponse createdUser = authClient.createPortalUser("name", "email", "pass");

authClient.updatePortalUser(createdUser.getId(), "email-100", "pass-100");

public LoginRequest() {
} // JAXB needs this
assert authClient.getPortalUser(createdUser.getId()).getEmail() == "email-100";
}

public LoginRequest(String name, String age) {
this.email = name;
this.password = age;
}
private static void testCreate() {
PortalUserResponse createdPortalUser = new AuthClient().createPortalUser("name", "email", "pass");
log.info("Creation was successful?: " + createdPortalUser);
}

@Override
public String toString() {
return "Email: " + email + "; Password: " + password;
}
private static void testLogin() {
PortalUserResponse user = new AuthClient().login(new LoginRequest("[email protected]", "samplePassword"));
log.info("Login is successful?: " + (user != null));

PortalUserResponse user2 = new AuthClient().login(new LoginRequest("[email protected]", "wrong-pass"));
log.info("Login is successful?: " + (user2 != null));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.cakecatalog.srv.auth.client.model;

public class CreatePortalUserRequest {
private String name;
private String email;
private String password;

public CreatePortalUserRequest(String name, String email, String password) {
this.setName(name);
this.setEmail(email);
this.setPassword(password);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.cakecatalog.srv.auth.client.model;

public class LoginRequest {
private String email;
private String password;

public LoginRequest() {
} // JAXB needs this

public LoginRequest(String name, String age) {
this.setEmail(name);
this.setPassword(age);
}

@Override
public String toString() {
return "Email: " + getEmail() + "; Password: " + getPassword();
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.cakecatalog.srv.auth.client.model;

public class PortalUserResponse {

private Long id;
private String name;
private String email;
private String password;

@Override
public String toString() {
return "PortalUserResponse{" +
"id=" + getId() +
", name='" + getName() + '\'' +
", email='" + getEmail() + '\'' +
'}';
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//@Configuration
//@EnableAutoConfiguration
//@ComponentScan
//@EnableJpaRepositories("com.cakecatalog.srv.auth.domain")
//@EntityScan("com.cakecatalog.srv.auth.domain")
@SpringBootApplication
public class CakeCatalogAuthSrvApplication {

//TODO define REST interface - login, logout, createUser, editUser, getUser

//TODO connect to PostgreSql DB
//TODO create services that uses repositories


//TODO move liquibase changelog to this app

public static void main(String[] args) {
SpringApplication.run(CakeCatalogAuthSrvApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.cakecatalog.srv.auth;

import com.cakecatalog.srv.auth.domain.PortalUser;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;

@Configuration
public class RepositoryConfig extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(PortalUser.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,52 @@ public class PortalUser {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
private Long id;

@Version
Long version;
private Long version;

public String name;
public String email;
public String password;
private String name;
private String email;
private String password;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getVersion() {
return version;
}

public void setVersion(Long version) {
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
Loading

0 comments on commit 82f232c

Please sign in to comment.