Skip to content
Snippets Groups Projects
Commit 23aa27f3 authored by Beth Davies's avatar Beth Davies
Browse files

Add API controller for profile page and unit test

parent 7a53207e
Branches
No related tags found
No related merge requests found
......@@ -25,14 +25,12 @@ public class ProfileController {
ModelAndView modelAndView = new ModelAndView("profile/profilePage");
String username = authentication.getName();
User user = userService.findByEmail(username);
Profile profile = profileService.getProfile(user.getId());
Profile profile = getProfileForAuthenticatedUser(authentication);
modelAndView.addObject("profile", profile);
return modelAndView;
}
@PostMapping("/update")
public String updateProfile(@ModelAttribute Profile profile, Authentication authentication) {
String username = authentication.getName();
......@@ -41,4 +39,11 @@ public class ProfileController {
profileService.updateProfile(profile);
return "redirect:/profile";
}
private Profile getProfileForAuthenticatedUser(Authentication authentication) {
String username = authentication.getName();
User user = userService.findByEmail(username);
Profile profile = profileService.getProfile(user.getId());
return profile;
}
}
package polish_community_group_11.polish_community.profile.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import polish_community_group_11.polish_community.profile.models.Profile;
import polish_community_group_11.polish_community.profile.services.ProfileService;
import polish_community_group_11.polish_community.register.models.User;
import polish_community_group_11.polish_community.register.services.UserService;
@RestController
public class ProfileRestController {
@Autowired
private UserService userService;
@Autowired
private ProfileService profileService;
@GetMapping("/profile-json")
public Profile getProfile(Authentication authentication) {
return getProfileForAuthenticatedUser(authentication);
}
private Profile getProfileForAuthenticatedUser(Authentication authentication) {
String username = authentication.getName();
User user = userService.findByEmail(username);
Profile profile = profileService.getProfile(user.getId());
return profile;
}
}
......@@ -30,9 +30,9 @@ Inspire Future Projects: Your participation could spark new ideas and motivate o
Stay Ahead of the Curve: By participating, you gain knowledge about the latest trends in science and technology, giving you an edge in academic and professional fields.
By joining this science fair, you are not only enriching your own learning experience but also contributing to a vibrant community of innovators and explorers.');
insert into event (event_title, event_description, location, event_date, event_time,user_id, event_poster_url,whyJoin,benefits)
values ('Games Fair', 'Gamers explore through the game fair', 'Bristol', current_date,current_time, 1, 'https://d1csarkz8obe9u.cloudfront.net/posterpreviews/game-event-poster-template-c54aaeed440befaacf79e6dd35deb8f5_screen.jpg?ts=1486132851',"Abc", "Def");
values ('Games Fair', 'Gamers explore through the game fair', 'Bristol', current_date,current_time, 1, 'https://d1csarkz8obe9u.cloudfront.net/posterpreviews/game-event-poster-template-c54aaeed440befaacf79e6dd35deb8f5_screen.jpg?ts=1486132851','Abc', 'Def');
insert into event (event_title, event_description, location, event_date, event_time,user_id, event_poster_url,whyJoin,benefits)
values ('Bikes Fair', 'Riders explore through the Ride fair', 'Newport', current_date,current_time, 1, 'https://d1csarkz8obe9u.cloudfront.net/posterpreviews/bike-fest-poster-design-template-fb1cc1ab4b2aee783f8ee75476c4c92d_screen.jpg?ts=1637012682',"Abc", "Def");
values ('Bikes Fair', 'Riders explore through the Ride fair', 'Newport', current_date,current_time, 1, 'https://d1csarkz8obe9u.cloudfront.net/posterpreviews/bike-fest-poster-design-template-fb1cc1ab4b2aee783f8ee75476c4c92d_screen.jpg?ts=1637012682','Abc', 'Def');
-- mock data for news
......@@ -90,32 +90,27 @@ INSERT INTO roles (role_name) VALUES ('USER');
-- insert a admin user into the table
INSERT INTO users (email, password, fullname, dob, role_id, organization) VALUES
("admin@gmail.com", "Password!", "Harri Daives", "2003-07-22", 1, "Ludek Polonia Wajiska"),
("user1@gmail.com", "Password123", "John Smith", "1998-04-15", 2, "Tech Solutions Ltd"),
("user2@gmail.com", "SecurePass1!", "Jane Doe", "1995-11-20", 2, "Global Innovations"),
("user3@gmail.com", "Pa$$w0rd", "Emily Johnson", "1990-02-10", 2, "Quick Logistics"),
("user4@gmail.com", "MySecureP@ss", "Michael Brown", "1988-06-30", 2, "Creative Works Inc."),
("user5@gmail.com", "Welcome123!", "Jessica Davis", "2001-08-12", 2, "EduTech Solutions"),
("user6@gmail.com", "P@ssword789", "Daniel Garcia", "1993-03-05", 2, "HealthCare Inc."),
("user7@gmail.com", "SuperSecure@", "Sophia Wilson", "1999-09-25", 2, "Food Supply Co."),
("user8@gmail.com", "Passw0rd!", "James Martinez", "2000-01-18", 2, "Tech Support Hub"),
("user9@gmail.com", "UserPass!234", "Isabella Taylor", "1996-05-22", 2, "Dynamic Solutions"),
("user10@gmail.com", "ChangeMe#1", "David Anderson", "1992-12-13", 2, "EcoGreen Enterprises");
('admin@gmail.com', 'Password!', 'Harri Daives', '2003-07-22', 1, 'Ludek Polonia Wajiska'),
('user1@gmail.com', 'Password123', 'John Smith', '1998-04-15', 2, 'Tech Solutions Ltd'),
('user2@gmail.com', 'SecurePass1!', 'Jane Doe', '1995-11-20', 2, 'Global Innovations'),
('user3@gmail.com', 'Pa$$w0rd', 'Emily Johnson', '1990-02-10', 2, 'Quick Logistics'),
('user4@gmail.com', 'MySecureP@ss', 'Michael Brown', '1988-06-30', 2, 'Creative Works Inc.'),
('user5@gmail.com', 'Welcome123!', 'Jessica Davis', '2001-08-12', 2, 'EduTech Solutions'),
('user6@gmail.com', 'P@ssword789', 'Daniel Garcia', '1993-03-05', 2, 'HealthCare Inc.'),
('user7@gmail.com', 'SuperSecure@', 'Sophia Wilson', '1999-09-25', 2, 'Food Supply Co.'),
('user8@gmail.com', 'Passw0rd!', 'James Martinez', '2000-01-18', 2, 'Tech Support Hub'),
('user9@gmail.com', 'UserPass!234', 'Isabella Taylor', '1996-05-22', 2, 'Dynamic Solutions'),
('user10@gmail.com', 'ChangeMe#1', 'David Anderson', '1992-12-13', 2, 'EcoGreen Enterprises');
INSERT INTO users (email, password, fullname, dob, role_id)
VALUES ('user@email.com', 'Abcdef1!', 'Jane Doe', '200-01-01', 1 );
UPDATE user_profile SET bio = 'Jane''s bio' WHERE user_id = (SELECT id from users where email = 'user@email.com');
-- insert the user and role id's of the created roles and user
INSERT INTO user_roles (user_id, role_id)
VALUES (1, 1);
# ALTER TABLE user_profile ADD CONSTRAINT foreign key polish_community.user_profile.user_profile_ibfk_1
# FOREIGN KEY (user_id) REFERENCES polish_community.users.id (id)
# ON UPDATE CASCADE;
CREATE TRIGGER UMustHaveProfile AFTER INSERT ON users FOR EACH ROW BEGIN INSERT INTO user_profile (user_id) VALUES (NEW.id);
-- Insert posts
INSERT INTO feed (post_image_url, post_title, post_description, post_time, user_id) VALUES
('https://example.com/image1.jpg', 'Post 1', 'Description for post 1', '2024-12-07', 1),
......
......@@ -159,3 +159,6 @@ CREATE TABLE IF NOT EXISTS user_profile (
ON DELETE CASCADE
ON UPDATE CASCADE
) engine = InnoDB;
CREATE TRIGGER UMustHaveProfile AFTER INSERT ON users FOR EACH ROW INSERT INTO user_profile (user_id) VALUES (NEW.id);
package polish_community_group_11.polish_community.profile.controllers;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import polish_community_group_11.polish_community.register.models.User;
import polish_community_group_11.polish_community.register.services.UserService;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment. RANDOM_PORT)
@AutoConfigureMockMvc
class ProfileControllerTest {
@Autowired
MockMvc mockMvc;
@Test
void profile() {
@WithUserDetails("user@email.com")
void whenUserCallsProfileEndpointThenTheirProfileIsReturned() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/profile-json")
.accept(MediaType.APPLICATION_JSON)
)
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
.andExpect(MockMvcResultMatchers.content().json("{\"email\": \"user@email.com\"}"))
.andExpect(MockMvcResultMatchers.content().json("{\"bio\": \"Jane's bio\"}"));
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment