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
167 changes: 167 additions & 0 deletions src/test/java/com/scalesec/vulnado/CommentTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package com.scalesec.vulnado;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class VulnadoApplicationTests {

@Test
public void contextLoads() {
throw new UnsupportedOperationException("Method not implemented");
}

@Test
public void Comment_Create_ShouldReturnComment() {
// Arrange
String username = "testUser";
String body = "testBody";
Comment mockComment = mock(Comment.class);
when(mockComment.commit()).thenReturn(true);

// Act
Comment result = Comment.create(username, body);

// Assert
assertNotNull("Comment should not be null", result);
assertEquals("Username should match", username, result.getUsername());
assertEquals("Body should match", body, result.getBody());
}

@Test(expected = BadRequest.class)
public void Comment_Create_ShouldThrowBadRequestWhenCommitFails() {
// Arrange
String username = "testUser";
String body = "testBody";
Comment mockComment = mock(Comment.class);
when(mockComment.commit()).thenReturn(false);

// Act
Comment.create(username, body);
}

@Test(expected = ServerError.class)
public void Comment_Create_ShouldThrowServerErrorOnException() {
// Arrange
String username = "testUser";
String body = "testBody";
Comment mockComment = mock(Comment.class);
when(mockComment.commit()).thenThrow(new SQLException("Database error"));

// Act
Comment.create(username, body);
}

@Test
public void Comment_FetchAll_ShouldReturnListOfComments() throws SQLException {
// Arrange
Connection mockConnection = mock(Connection.class);
Statement mockStatement = mock(Statement.class);
ResultSet mockResultSet = mock(ResultSet.class);

when(mockConnection.createStatement()).thenReturn(mockStatement);
when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(true, false);
when(mockResultSet.getString("id")).thenReturn(UUID.randomUUID().toString());
when(mockResultSet.getString("username")).thenReturn("testUser");
when(mockResultSet.getString("body")).thenReturn("testBody");
when(mockResultSet.getTimestamp("created_on")).thenReturn(new Timestamp(System.currentTimeMillis()));

Postgres.setConnection(mockConnection);

// Act
List<Comment> comments = Comment.fetchAll();

// Assert
assertNotNull("Comments list should not be null", comments);
assertEquals("Comments list should contain one comment", 1, comments.size());
}

@Test
public void Comment_Delete_ShouldReturnTrueWhenSuccessful() throws SQLException {
// Arrange
String commentId = UUID.randomUUID().toString();
Connection mockConnection = mock(Connection.class);
PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);

when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeUpdate()).thenReturn(1);

Postgres.setConnection(mockConnection);

// Act
boolean result = Comment.delete(commentId);

// Assert
assertTrue("Delete should return true when successful", result);
}

@Test
public void Comment_Delete_ShouldReturnFalseWhenUnsuccessful() throws SQLException {
// Arrange
String commentId = UUID.randomUUID().toString();
Connection mockConnection = mock(Connection.class);
PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);

when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeUpdate()).thenReturn(0);

Postgres.setConnection(mockConnection);

// Act
boolean result = Comment.delete(commentId);

// Assert
assertFalse("Delete should return false when unsuccessful", result);
}

@Test
public void Comment_Commit_ShouldReturnTrueWhenSuccessful() throws SQLException {
// Arrange
Comment comment = new Comment(UUID.randomUUID().toString(), "testUser", "testBody", new Timestamp(System.currentTimeMillis()));
Connection mockConnection = mock(Connection.class);
PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);

when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeUpdate()).thenReturn(1);

Postgres.setConnection(mockConnection);

// Act
boolean result = comment.commit();

// Assert
assertTrue("Commit should return true when successful", result);
}

@Test
public void Comment_Commit_ShouldReturnFalseWhenUnsuccessful() throws SQLException {
// Arrange
Comment comment = new Comment(UUID.randomUUID().toString(), "testUser", "testBody", new Timestamp(System.currentTimeMillis()));
Connection mockConnection = mock(Connection.class);
PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);

when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeUpdate()).thenReturn(0);

Postgres.setConnection(mockConnection);

// Act
boolean result = comment.commit();

// Assert
assertFalse("Commit should return false when unsuccessful", result);
}
}
125 changes: 125 additions & 0 deletions src/test/java/com/scalesec/vulnado/CommentsControllerTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.scalesec.vulnado;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentsControllerTests {

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

@Test
public void comments_ShouldReturnAllComments() {
// Arrange
CommentsController controller = new CommentsController();
String token = "valid-token";
List<Comment> mockComments = new ArrayList<>();
mockComments.add(new Comment("user1", "This is a comment."));
mockComments.add(new Comment("user2", "Another comment."));
mockStatic(Comment.class);
when(Comment.fetch_all()).thenReturn(mockComments);

// Act
List<Comment> result = controller.comments(token);

// Assert
assertNotNull("Result should not be null", result);
assertEquals("Result size should match", 2, result.size());
assertEquals("First comment username should match", "user1", result.get(0).getUsername());
assertEquals("Second comment body should match", "Another comment.", result.get(1).getBody());
}

@Test
public void createComment_ShouldCreateAndReturnComment() {
// Arrange
CommentsController controller = new CommentsController();
String token = "valid-token";
CommentRequest input = new CommentRequest();
input.setUsername("user1");
input.setBody("This is a new comment.");
Comment mockComment = new Comment("user1", "This is a new comment.");
mockStatic(Comment.class);
when(Comment.create(input.getUsername(), input.getBody())).thenReturn(mockComment);

// Act
Comment result = controller.createComment(token, input);

// Assert
assertNotNull("Result should not be null", result);
assertEquals("Username should match", "user1", result.getUsername());
assertEquals("Body should match", "This is a new comment.", result.getBody());
}

@Test
public void deleteComment_ShouldDeleteAndReturnTrue() {
// Arrange
CommentsController controller = new CommentsController();
String token = "valid-token";
String commentId = "123";
mockStatic(Comment.class);
when(Comment.delete(commentId)).thenReturn(true);

// Act
Boolean result = controller.deleteComment(token, commentId);

// Assert
assertTrue("Result should be true", result);
}

@Test(expected = ResponseStatusException.class)
public void comments_ShouldThrowExceptionForInvalidToken() {
// Arrange
CommentsController controller = new CommentsController();
String invalidToken = "invalid-token";
mockStatic(User.class);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED)).when(User.class);
User.assertAuth(secret, invalidToken);

// Act
controller.comments(invalidToken);
}

@Test(expected = ResponseStatusException.class)
public void createComment_ShouldThrowExceptionForInvalidToken() {
// Arrange
CommentsController controller = new CommentsController();
String invalidToken = "invalid-token";
CommentRequest input = new CommentRequest();
input.setUsername("user1");
input.setBody("This is a new comment.");
mockStatic(User.class);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED)).when(User.class);
User.assertAuth(secret, invalidToken);

// Act
controller.createComment(invalidToken, input);
}

@Test(expected = ResponseStatusException.class)
public void deleteComment_ShouldThrowExceptionForInvalidToken() {
// Arrange
CommentsController controller = new CommentsController();
String invalidToken = "invalid-token";
String commentId = "123";
mockStatic(User.class);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED)).when(User.class);
User.assertAuth(secret, invalidToken);

// Act
controller.deleteComment(invalidToken, commentId);
}
}
64 changes: 64 additions & 0 deletions src/test/java/com/scalesec/vulnado/CowControllerTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.scalesec.vulnado;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class VulnadoApplicationTests {

@Autowired
private TestRestTemplate restTemplate;

@Test
public void contextLoads() {
// Ensures the application context loads successfully
assertNotNull("Application context should load", restTemplate);
}

@Test
public void cowsay_GetRequest_ShouldReturnDefaultMessage() {
// Test the GET request with default input
ResponseEntity<String> response = restTemplate.getForEntity("/cowsay", String.class);
assertEquals("Response status should be 200", 200, response.getStatusCodeValue());
assertNotNull("Response body should not be null", response.getBody());
assertEquals("Response should match default message", Cowsay.run("I love Linux!"), response.getBody());
}

@Test
public void cowsay_GetRequest_ShouldReturnCustomMessage() {
// Test the GET request with custom input
String customMessage = "Hello, World!";
ResponseEntity<String> response = restTemplate.getForEntity("/cowsay?input=" + customMessage, String.class);
assertEquals("Response status should be 200", 200, response.getStatusCodeValue());
assertNotNull("Response body should not be null", response.getBody());
assertEquals("Response should match custom message", Cowsay.run(customMessage), response.getBody());
}

@Test
public void cowsay_PostRequest_ShouldReturnDefaultMessage() {
// Test the POST request with default input
ResponseEntity<String> response = restTemplate.postForEntity("/cowsay", null, String.class);
assertEquals("Response status should be 200", 200, response.getStatusCodeValue());
assertNotNull("Response body should not be null", response.getBody());
assertEquals("Response should match default message", Cowsay.run("I love Linux!"), response.getBody());
}

@Test
public void cowsay_PostRequest_ShouldReturnCustomMessage() {
// Test the POST request with custom input
String customMessage = "Spring Boot is awesome!";
ResponseEntity<String> response = restTemplate.postForEntity("/cowsay?input=" + customMessage, null, String.class);
assertEquals("Response status should be 200", 200, response.getStatusCodeValue());
assertNotNull("Response body should not be null", response.getBody());
assertEquals("Response should match custom message", Cowsay.run(customMessage), response.getBody());
}
}
Loading