Merge branch 'tests' into 'dev'

Tests

See merge request sa4-2020/the-sanmarinoes/backend!202
This commit is contained in:
Claudio Maggioni 2020-05-27 11:33:02 +02:00
commit c8470d7a5d
2 changed files with 38 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.function.Function; import java.util.function.Function;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -18,6 +19,7 @@ public class JWTTokenUtils {
/** The secret key used to encrypt all JWTs */ /** The secret key used to encrypt all JWTs */
@Value("${jwt.secret}") @Value("${jwt.secret}")
@Setter
private String secret; private String secret;
/** /**

View file

@ -0,0 +1,36 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.userdetails.UserDetails;
@ExtendWith({MockitoExtension.class})
public class JWTTokenUtilsTests {
@InjectMocks private JWTTokenUtils utils;
@Mock private UserDetails userDetails;
@Test
public void testGenerateToken() {
utils.setSecret(
"One, seven, three, four, six, seven\n"
+ "Three, two, one, four, seven, six, charlie, three\n"
+ "Two, seven, eight, nine, seven, seven, seven\n"
+ "Six, four, three, tango, seven, three, two, victor, seven\n"
+ "Three, one, one, seven, eight, eight, eight, seven, three\n"
+ "Two, four, seven, six, seven, eight, nine\n"
+ "Seven, six, four, three, seven, six\n"
+ "Lock");
when(userDetails.getUsername()).thenReturn("username");
String token = utils.generateToken(userDetails);
assertThat(token).isNotNull();
assertThat(utils.validateToken(token, userDetails)).isTrue();
assertThat(utils.getUsernameFromToken(token)).isEqualTo("username");
}
}