Skip to content
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
8 changes: 8 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ pipeline {
}
}
}

stage("Maven: Run Tests") {
steps {
script {
sh 'mvn test'
}
}
}

stage("Trivy: Filesystem scan"){
steps{
Expand Down
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -84,6 +89,25 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
147 changes: 147 additions & 0 deletions src/test/java/com/example/bankapp/config/SecurityConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.example.bankapp.config;

import com.example.bankapp.model.Account;
import com.example.bankapp.service.AccountService;
import org.junit.jupiter.api.BeforeEach;
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.boot.test.mock.mockito.MockBean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;

import java.math.BigDecimal;
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
class SecurityConfigTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private PasswordEncoder passwordEncoder;

@MockBean
private AccountService accountService;

private Account testAccount;

@BeforeEach
void setUp() {
testAccount = new Account();
testAccount.setId(1L);
testAccount.setUsername("testuser");
testAccount.setPassword("password");
testAccount.setBalance(new BigDecimal("1000.00"));
testAccount.setTransactions(new ArrayList<>());

when(accountService.findAccountByUsername(anyString())).thenReturn(testAccount);
when(accountService.getTransactionHistory(testAccount)).thenReturn(new ArrayList<>());
}

@Test
void passwordEncoder_ShouldReturnBCryptPasswordEncoder() {
assertNotNull(passwordEncoder);
String rawPassword = "testPassword123";
String encodedPassword = passwordEncoder.encode(rawPassword);

assertNotEquals(rawPassword, encodedPassword);
assertTrue(passwordEncoder.matches(rawPassword, encodedPassword));
}

@Test
void registerPage_ShouldBeAccessibleWithoutAuthentication() throws Exception {
mockMvc.perform(get("/register"))
.andExpect(status().isOk());
}

@Test
void loginPage_ShouldBeAccessibleWithoutAuthentication() throws Exception {
mockMvc.perform(get("/login"))
.andExpect(status().isOk());
}

@Test
void dashboard_ShouldRequireAuthentication() throws Exception {
mockMvc.perform(get("/dashboard"))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrlPattern("**/login"));
}

@Test
void transactions_ShouldRequireAuthentication() throws Exception {
mockMvc.perform(get("/transactions"))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrlPattern("**/login"));
}

@Test
@WithMockUser(username = "testuser")
void dashboard_ShouldBeAccessibleWhenAuthenticated() throws Exception {
mockMvc.perform(get("/dashboard"))
.andExpect(status().isOk());
}

@Test
@WithMockUser(username = "testuser")
void transactions_ShouldBeAccessibleWhenAuthenticated() throws Exception {
mockMvc.perform(get("/transactions"))
.andExpect(status().isOk());
}

@Test
void logout_ShouldRedirectToLoginPage() throws Exception {
mockMvc.perform(post("/logout").with(csrf()))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/login?logout"));
}

@Test
void loginProcessingUrl_ShouldBeConfigured() throws Exception {
mockMvc.perform(post("/login")
.with(csrf())
.param("username", "testuser")
.param("password", "password"))
.andExpect(status().is3xxRedirection());
}

@Test
void passwordEncoder_ShouldHandleEmptyPassword() {
String emptyPassword = "";
String encodedPassword = passwordEncoder.encode(emptyPassword);

assertNotNull(encodedPassword);
assertTrue(passwordEncoder.matches(emptyPassword, encodedPassword));
}

@Test
void passwordEncoder_ShouldHandleSpecialCharacters() {
String specialPassword = "p@$$w0rd!#$%^&*()";
String encodedPassword = passwordEncoder.encode(specialPassword);

assertTrue(passwordEncoder.matches(specialPassword, encodedPassword));
}

@Test
void passwordEncoder_ShouldGenerateDifferentHashesForSamePassword() {
String password = "samePassword";
String encoded1 = passwordEncoder.encode(password);
String encoded2 = passwordEncoder.encode(password);

assertNotEquals(encoded1, encoded2);
assertTrue(passwordEncoder.matches(password, encoded1));
assertTrue(passwordEncoder.matches(password, encoded2));
}
}
Loading