forked from AikidoSec/firewall-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetUserTest.java
More file actions
141 lines (130 loc) · 5.35 KB
/
Copy pathSetUserTest.java
File metadata and controls
141 lines (130 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import dev.aikido.agent_api.SetUser;
import dev.aikido.agent_api.context.Context;
import dev.aikido.agent_api.context.ContextObject;
import dev.aikido.agent_api.thread_cache.ThreadCache;
import org.junit.jupiter.api.*;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.junitpioneer.jupiter.StdIo;
import org.junitpioneer.jupiter.StdOut;
import java.io.IOException;
import java.sql.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static utils.EmtpyThreadCacheObject.getEmptyThreadCacheObject;
@SetEnvironmentVariable(key = "AIKIDO_LOG_LEVEL", value = "trace")
@SetEnvironmentVariable(key = "AIKIDO_TOKEN", value = "invalid-token-2")
public class SetUserTest {
public static class SampleContextObject extends ContextObject {
public SampleContextObject() {
this.method = "GET";
this.source = "web";
this.url = "https://example.com/api/resource";
this.route = "/api/resource";
this.remoteAddress = "192.168.1.1";
this.headers = new HashMap<>();
this.query = new HashMap<>();
this.cookies = new HashMap<>();
this.body = "{\"key\":\"value\"}"; // Body as a JSON string
this.executedMiddleware = true; // Start with "executed middleware" as true
}
}
@BeforeAll
public static void clean() {
Context.set(null);
ThreadCache.set(null);
};
@BeforeEach
public void setUp() throws SQLException {
ThreadCache.set(getEmptyThreadCacheObject());
}
@AfterEach
public void tearDown() throws SQLException {
Context.set(null);
ThreadCache.set(null);
}
@Test
@StdIo
public void testIndependenceFromThreadCacheSet(StdOut out) throws SQLException, IOException {
Context.set(new SampleContextObject());
// Test with thread cache set :
SetUser.setUser(new SetUser.UserObject("ID", "Name"));
assertTrue(out.capturedString().contains("setUser(...) must be called before the Zen middleware is executed."));
}
@Test
@StdIo
public void testIndependenceFromThreadCacheNull(StdOut out) throws SQLException, IOException {
// Test with thread cache set to null:
SetUser.setUser(new SetUser.UserObject("ID", "Name"));
assertFalse(out.capturedString().contains("setUser(...) must be called before the Zen middleware is executed."));
}
@Test
@StdIo
public void testValidAndInvalidUsers1(StdOut out) throws SQLException {
Context.set(new SampleContextObject());
// Test with invalid user 1 :
SetUser.setUser(new SetUser.UserObject("", "Name"));
assertTrue(out.capturedString().contains("User ID or name cannot be empty."));
}
@Test
@StdIo
public void testValidAndInvalidUsers2(StdOut out) throws SQLException {
// Test with invalid user 2 :
SetUser.setUser(new SetUser.UserObject("ID", ""));
assertTrue(out.capturedString().contains("User ID or name cannot be empty."));
}
@Test
@StdIo
public void testValidAndInvalidUsers3(StdOut out) throws SQLException {
// Test with invalid user 3 :
SetUser.setUser(new SetUser.UserObject("", ""));
assertTrue(out.capturedString().contains("User ID or name cannot be empty."));
}
@Test
@StdIo
public void testValidAndInvalidUsers4(StdOut out) throws SQLException {
// Test with invalid user 4 :
SetUser.setUser(new SetUser.UserObject(null, ""));
assertTrue(out.capturedString().contains("User ID or name cannot be empty."));
}
@Test
@StdIo
public void testValidAndInvalidUsers5(StdOut out) throws SQLException {
// Test with invalid user 5 :
SetUser.setUser(new SetUser.UserObject(null, null));
assertTrue(out.capturedString().contains("User ID or name cannot be empty."));
}
@Test
@StdIo
public void testValidAndInvalidUsers6(StdOut out) throws SQLException {
// Test with invalid user 6 :
SetUser.setUser(new SetUser.UserObject("ID", null));
assertTrue(out.capturedString().contains("User ID or name cannot be empty."));
}
@Test
@StdIo
public void testValidUser(StdOut out) throws SQLException {
// Test with valid user :
SetUser.setUser(new SetUser.UserObject("ID", "Name"));
assertFalse(out.capturedString().contains("setUser(...) must be called before the Zen middleware is executed."));
}
@Test
@StdIo
public void testWithContextNotSet(StdOut out) throws SQLException {
// Test with context not set :
SetUser.setUser(new SetUser.UserObject("ID", "Name"));
assertEquals(0, out.capturedString().length());
// Test with context not set and user invalid :
SetUser.setUser(new SetUser.UserObject("ID", null));
assertTrue(out.capturedString().contains("User ID or name cannot be empty."));
}
@Test
@StdIo
public void testWithContextSetButNotExecutedMiddleware(StdOut out) throws SQLException {
// Test with context set but executed middleware false:
ContextObject ctx = new SampleContextObject();
ctx.setExecutedMiddleware(false);
Context.set(ctx);
SetUser.setUser(new SetUser.UserObject("ID", "Name"));
assertFalse(out.capturedString().contains("SetUser")); // Should not contain SetUser class
}
}