diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java index 78e5e5e..0457a51 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtils.java @@ -6,6 +6,7 @@ import io.jsonwebtoken.SignatureAlgorithm; import java.util.Date; import java.util.HashMap; import java.util.function.Function; +import lombok.Setter; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Component; @@ -18,6 +19,7 @@ public class JWTTokenUtils { /** The secret key used to encrypt all JWTs */ @Value("${jwt.secret}") + @Setter private String secret; /** diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtilsTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtilsTests.java new file mode 100644 index 0000000..0f2a6da --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/JWTTokenUtilsTests.java @@ -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"); + } +}