|
2 | 2 |
|
3 | 3 | import com.mongodb.client.MongoClient; |
4 | 4 | import com.mongodb.client.MongoClients; |
| 5 | +import com.mongodb.client.MongoCollection; |
| 6 | +import org.bson.Document; |
| 7 | +import org.bson.types.ObjectId; |
5 | 8 | import org.evomaster.client.java.controller.ExternalSutController; |
6 | 9 | import org.evomaster.client.java.controller.InstrumentedSutStarter; |
7 | 10 | import org.evomaster.client.java.controller.api.dto.AuthenticationDto; |
| 11 | +import org.evomaster.client.java.controller.api.dto.JsonTokenPostLoginDto; |
8 | 12 | import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
9 | 13 | import org.evomaster.client.java.sql.DbSpecification; |
10 | 14 | import org.evomaster.client.java.controller.problem.ProblemInfo; |
11 | 15 | import org.evomaster.client.java.controller.problem.RestProblem; |
12 | 16 | import org.testcontainers.containers.GenericContainer; |
13 | 17 |
|
| 18 | +import java.util.Arrays; |
14 | 19 | import java.util.Collections; |
15 | 20 | import java.util.List; |
16 | 21 |
|
@@ -61,12 +66,17 @@ public static void main(String[] args) { |
61 | 66 | //https://www.mongodb.com/docs/drivers/java/sync/current/compatibility/ |
62 | 67 | private static final String MONGODB_VERSION = "4.4"; |
63 | 68 |
|
64 | | - private static final String MONGODB_DATABASE_NAME = "Reservations"; |
| 69 | + private static final String MONGODB_DATABASE_NAME = "reservations-api"; |
65 | 70 |
|
66 | | - private static final GenericContainer mongodbContainer = new GenericContainer("mongo:" + MONGODB_VERSION) |
67 | | - .withTmpFs(Collections.singletonMap("/data/db", "rw")) |
| 71 | + private static final GenericContainer mongodbContainer = new GenericContainer("bitnami/mongodb:" + MONGODB_VERSION) |
| 72 | + .withTmpFs(Collections.singletonMap("/bitnami/mongodb", "rw")) |
| 73 | + .withEnv("MONGODB_REPLICA_SET_MODE", "primary") |
| 74 | + .withEnv("ALLOW_EMPTY_PASSWORD", "yes") |
68 | 75 | .withExposedPorts(MONGODB_PORT); |
69 | 76 |
|
| 77 | + private static final String rawPassword = "bar123"; |
| 78 | + private static final String hashedPassword = "$2a$10$nEDY5j731yXGnQHyM39PWurJWr1FukegmKYYarK5WOoAMmgDs6D3u"; |
| 79 | + |
70 | 80 | private String mongoDbUrl; |
71 | 81 |
|
72 | 82 | private MongoClient mongoClient; |
@@ -103,7 +113,8 @@ public String[] getInputParameters() { |
103 | 113 | return new String[]{ |
104 | 114 | "--server.port=" + sutPort, |
105 | 115 | "--databaseUrl="+mongoDbUrl, |
106 | | - "--spring.data.mongodb.uri="+mongoDbUrl |
| 116 | + "--spring.data.mongodb.uri="+mongoDbUrl, |
| 117 | + "--app.jwt.secret=abcdef012345678901234567890123456789abcdef012345678901234567890123456789" |
107 | 118 | }; |
108 | 119 | } |
109 | 120 |
|
@@ -145,6 +156,34 @@ public void postStart() { |
145 | 156 | @Override |
146 | 157 | public void resetStateOfSUT() { |
147 | 158 | mongoClient.getDatabase(MONGODB_DATABASE_NAME).drop(); |
| 159 | + |
| 160 | + mongoClient.getDatabase(MONGODB_DATABASE_NAME).createCollection("users"); |
| 161 | + |
| 162 | + MongoCollection<Document> users = mongoClient.getDatabase(MONGODB_DATABASE_NAME).getCollection("users"); |
| 163 | + users.insertMany(Arrays.asList( |
| 164 | + new Document() |
| 165 | + .append("_id", new ObjectId()) |
| 166 | + .append("_class", "sk.cyrilgavala.reservationsApi.model.User") |
| 167 | + .append("username", "foo") |
| 168 | + .append("email", "foo@foo.com") |
| 169 | + .append("password", hashedPassword) |
| 170 | + .append("role", "USER"), |
| 171 | + new Document() |
| 172 | + .append("_id", new ObjectId()) |
| 173 | + .append("_class", "sk.cyrilgavala.reservationsApi.model.User") |
| 174 | + .append("username", "bar") |
| 175 | + .append("email", "bar@foo.com") |
| 176 | + .append("password", hashedPassword) |
| 177 | + .append("role", "USER"), |
| 178 | + new Document() |
| 179 | + .append("_id", new ObjectId()) |
| 180 | + .append("_class", "sk.cyrilgavala.reservationsApi.model.User") |
| 181 | + .append("username", "admin") |
| 182 | + .append("email", "admin@foo.com") |
| 183 | + .append("password", hashedPassword) |
| 184 | + .append("role", "ADMIN") |
| 185 | + )); |
| 186 | + |
148 | 187 | } |
149 | 188 |
|
150 | 189 | @Override |
@@ -180,7 +219,39 @@ public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
180 | 219 |
|
181 | 220 | @Override |
182 | 221 | public List<AuthenticationDto> getInfoForAuthentication() { |
183 | | - return null; |
| 222 | + |
| 223 | + return Arrays.asList( |
| 224 | + new AuthenticationDto() {{ |
| 225 | + name = "admin"; |
| 226 | + jsonTokenPostLogin = new JsonTokenPostLoginDto() {{ |
| 227 | + userId = "admin"; |
| 228 | + endpoint = "/api/user/login"; |
| 229 | + jsonPayload = "{\"username\":\"admin\", \"password\":\""+rawPassword+"\"}"; |
| 230 | + extractTokenField = "/accessToken"; |
| 231 | + headerPrefix = "Bearer "; |
| 232 | + }}; |
| 233 | + }}, |
| 234 | + new AuthenticationDto() {{ |
| 235 | + name = "foo"; |
| 236 | + jsonTokenPostLogin = new JsonTokenPostLoginDto() {{ |
| 237 | + userId = "foo"; |
| 238 | + endpoint = "/api/user/login"; |
| 239 | + jsonPayload = "{\"username\":\"foo\", \"password\":\""+rawPassword+"\"}"; |
| 240 | + extractTokenField = "/accessToken"; |
| 241 | + headerPrefix = "Bearer "; |
| 242 | + }}; |
| 243 | + }}, |
| 244 | + new AuthenticationDto() {{ |
| 245 | + name = "bar"; |
| 246 | + jsonTokenPostLogin = new JsonTokenPostLoginDto() {{ |
| 247 | + userId = "bar"; |
| 248 | + endpoint = "/api/user/login"; |
| 249 | + jsonPayload = "{\"username\":\"bar\", \"password\":\""+rawPassword+"\"}"; |
| 250 | + extractTokenField = "/accessToken"; |
| 251 | + headerPrefix = "Bearer "; |
| 252 | + }}; |
| 253 | + }} |
| 254 | + ); |
184 | 255 | } |
185 | 256 |
|
186 | 257 | @Override |
|
0 commit comments