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

Accessibility #153

Open
wants to merge 3 commits into
base: master
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nonononoki.alovoa.config;

import org.springframework.core.env.Profiles;

public class ProfileConstants {

public final static Profiles TEST = Profiles.of("test");

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand Down Expand Up @@ -33,6 +34,9 @@
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private Environment environment;

@Value("${app.text.key}")
private String key;

Expand Down Expand Up @@ -86,7 +90,9 @@ public void configure(HttpSecurity http) throws Exception {
http.sessionManagement().maximumSessions(10).expiredSessionStrategy(getSessionInformationExpiredStrategy())
.sessionRegistry(sessionRegistry());
http.csrf().ignoringAntMatchers("/donate/received/**");
http.requiresChannel().anyRequest().requiresSecure();
if (!environment.acceptsProfiles(ProfileConstants.TEST)) {
http.requiresChannel().anyRequest().requiresSecure();
}
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ profile.warning.no-gender-preference=No gender preference found!
profile.warning.no-location=No location found! It will be set automatically when you search for other users!
profile.warning.intention.limit=Intention changed. You cannot change it for another 24 hours.

profile.button.like=Like
profile.button.dislike=Dislike

delete-account.title=Delete account
delete-account.confirm=I hereby confirm to delete this account permanently.
delete-account.not-active=Link is not active yet. Please check the instructions we have sent you or send another account deletion request.
Expand Down
20 changes: 14 additions & 6 deletions src/main/resources/templates/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,15 @@
<div class="right" th:if="${user.compatible}" style="display: flex;">
<button class="button colored is-danger"
style="margin-right: 4px;"
th:onclick="hideUser([[${user.idEncoded}]])">
th:onclick="hideUser([[${user.idEncoded}]])"
th:title="#{profile.button.dislike}"
th:attr="aria-label=#{profile.button.dislike}">
<i class="fa fa-thumbs-down"></i>
</button>
<div class="search-button-padding"></div>
<button class="button colored is-success"
th:onclick="likeUser([[${user.idEncoded}]])">
th:onclick="likeUser([[${user.idEncoded}]])"
th:title="#{profile.button.like}" th:attr="aria-label=#{profile.button.like}">
<i class="fa fa-thumbs-up"></i>
</button>
</div>
Expand Down Expand Up @@ -723,23 +726,28 @@
<div id="like-hide-bottom" th:if="${compatible}" >
<button class="button colored is-danger" style="height: 56px; width: 56px;"
th:unless="${user.hiddenByCurrentUser or user.likedByCurrentUser}"
th:onclick="hideUser([[${user.idEncoded}]])">
th:onclick="hideUser([[${user.idEncoded}]])"
th:title="#{profile.button.dislike}" th:attr="aria-label=#{profile.button.dislike}">
<i class="fa fa-thumbs-down"></i>
</button>
<button class="button colored is-danger" style="height: 56px; width: 56px;"
th:if="${user.hiddenByCurrentUser or user.likedByCurrentUser}"
th:onclick="hideUser([[${user.idEncoded}]])" disabled>
th:onclick="hideUser([[${user.idEncoded}]])"
th:title="#{profile.button.dislike}" th:attr="aria-label=#{profile.button.dislike}"
disabled>
<i class="fa fa-thumbs-down"></i>
</button>
<div style="padding-right: 24px;"></div>
<button class="button colored is-success" style="height: 56px; width: 56px;"
th:unless="${user.likedByCurrentUser}"
th:onclick="likeUser([[${user.idEncoded}]])">
th:onclick="likeUser([[${user.idEncoded}]])"
th:title="#{profile.button.like}" th:attr="aria-label=#{profile.button.like}">
<i class="fa fa-thumbs-up"></i>
</button>
<button class="button colored is-success" style="height: 56px; width: 56px;"
th:if="${user.likedByCurrentUser}"
th:onclick="likeUser([[${user.idEncoded}]])" disabled>
th:onclick="likeUser([[${user.idEncoded}]])" disabled
th:title="#{profile.button.like}" th:attr="aria-label=#{profile.button.like}">
<i class="fa fa-thumbs-up"></i>
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.nonononoki.alovoa.html;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;

Expand All @@ -10,9 +13,12 @@
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.transaction.annotation.Transactional;

import com.nonononoki.alovoa.component.TextEncryptorConverter;
Expand All @@ -26,9 +32,11 @@
import com.nonononoki.alovoa.service.RegisterService;
import com.nonononoki.alovoa.service.RegisterServiceTest;
import com.nonononoki.alovoa.service.UserService;
import com.nonononoki.alovoa.util.AuthTestUtil;

@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureMockMvc
@Transactional
class UserProfileResourceTest {

Expand Down Expand Up @@ -67,6 +75,9 @@ class UserProfileResourceTest {
@Autowired
private TextEncryptorConverter textEncryptor;

@Autowired
private MockMvc mockMvc;

@BeforeEach
void before() throws Exception {
Mockito.when(mailService.sendMail(Mockito.any(String.class), any(String.class), any(String.class),
Expand Down Expand Up @@ -97,4 +108,17 @@ void test() throws Exception {
String encodedId = UserDto.encodeId(user.getId(), textEncryptor);
userProfileResource.profileView(encodedId);
}

@Test
void profileView__validUser__profileDisplayed() throws Exception {
Mockito.when(authService.getCurrentUser()).thenReturn(testUsers.get(0));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit silly as we really don't need to mock the AuthService once we actually have working auth in tests.
If we don't mock the AuthService then the ApplicationContext can't be reused though.
But I think the @AutoConfigureMockMvc will change the ApplicationContext hash anyways, since it adds the MockMvc to the context.
My suggestion would be to move all the @SpringBootTest classes to this pattern.

AuthTestUtil.setAuthTo(testUsers.get(0));

ResultActions ra = mockMvc.perform(get("/profile/view/" + UserDto.encodeId(testUsers.get(1).getId(), textEncryptor)));

ra.andExpect(status().isOk());
String contentAsString = ra.andReturn().getResponse().getContentAsString();
assertThat(contentAsString.contains("aria-label=\"Like\""));
assertThat(contentAsString.contains(testUsers.get(1).getFirstName())).withFailMessage("Profile must contain first name of profile user");
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/nonononoki/alovoa/util/AuthTestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.nonononoki.alovoa.util;

import java.util.Collection;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import com.nonononoki.alovoa.entity.User;
import com.nonononoki.alovoa.service.AuthService;

public class AuthTestUtil {

/**
* See {@link #setAuthTo(User, String, Collection)}
*
* @see #setAuthTo(User, String, Collection)
*/
public static void setAuthTo(User user) {
setAuthTo(user, user.getPassword(), user.getAuthorities());
}

/**
* Sets the currently logged in user to the provided user.
* The user doesn't necessarily need to exist but calls to
* {@link AuthService#getCurrentUser()} will return null then.
*
* @param user User to set as current User
* @param password Password user by the User
* @param authorities Roles granted to the User
*/
public static void setAuthTo(User user, String password, Collection<? extends GrantedAuthority> authorities) {
Authentication auth = new UsernamePasswordAuthenticationToken(user, password,
authorities);
SecurityContextHolder.getContext().setAuthentication(auth);
}
}