README.md 1.85 KB
Newer Older
Sonia Zorba's avatar
Sonia Zorba committed
1
# VOSpace REST service
Sara Bertocco's avatar
Sara Bertocco committed
2

Sonia Zorba's avatar
Sonia Zorba committed
3
**⚠ Work in progress! ⚠**
Sara Bertocco's avatar
Sara Bertocco committed
4

Sonia Zorba's avatar
Sonia Zorba committed
5
6
7
8
9
## Developer notes

### DAO testing

This VOSpace implementation shares the database with the [VOSpace Transfer Service application](https://www.ict.inaf.it/gitlab/vospace/vospace-transfer-service). The structure of the database is defined in a [separate repository](https://www.ict.inaf.it/gitlab/vospace/vospace-file-catalog). To avoid duplicating database definitions, DAO test classes load the database directly from the files of that repository. We assume that when running the tests the git repository exists and it is located in the same parent folder containing this repository.
Sara Bertocco's avatar
Sara Bertocco committed
10

Sonia Zorba's avatar
Sonia Zorba committed
11
To reconfigure the path of that repository edit the property `init_database_scripts_path` in test.properties.
12

Sonia Zorba's avatar
Sonia Zorba committed
13
### Loading fake users in MockMvc
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

Test classes annotated with `@SpringBootTest` and `@AutoConfigureMockMvc` can be used to test REST controllers. Theoretically it should be possible configure a fake principal to each test request using the following notation:

```java
mockMvc.perform(post("/endpoint").principal(myFakeUser));
```

However it seems that the method is ignored if the principal is set using a custom servlet filter, like in our case (see `TokenFilter` registration defined in `VospaceApplication` class).

To bypass the problem a fake `TokenFilter` has been defined in `TokenFilterConfig` test class. This filter returns some fake users based on the received fake token. If you need additional test users just add them in the `getFakeUser()` method.

To use the fake filter add the following annotations to the test class:

```java
@ContextConfiguration(classes = {TokenFilterConfig.class})
@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
```

Then add the fake token to the test request:

```java
mockMvc.perform(post("/endpoint").header("Authorization", "Bearer user1_token"));
```