Newer
Older
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
package it.inaf.ia2.transfer.controller;
import it.inaf.ia2.aa.jwt.TokenParser;
import it.inaf.ia2.transfer.auth.GmsClient;
import it.inaf.ia2.transfer.persistence.FileDAO;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.assertj.core.util.Files;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
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.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = {"path_prefix=/"})
public class GetFileControllerTest {
@MockBean
private TokenParser tokenParser;
@MockBean
private GmsClient gmsClient;
@MockBean
private FileDAO fileDao;
@Autowired
private MockMvc mockMvc;
private File tempFile;
@BeforeEach
public void init() {
tempFile = Files.newTemporaryFile();
}
@AfterEach
public void cleanup() {
tempFile.delete();
}
@Test
public void getPublicFile() throws Exception {
FileInfo fileInfo = new FileInfo();
fileInfo.setOsRelPath(tempFile.getAbsolutePath());
fileInfo.setIsPublic(true);
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
.andDo(print())
.andExpect(status().isOk());
}
@Test
public void testFileNotFoundInDb() throws Exception {
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
public void testFileNotFoundOnDisk() throws Exception {
FileInfo fileInfo = new FileInfo();
fileInfo.setOsRelPath("/this/doesnt/exists");
fileInfo.setIsPublic(true);
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
mockMvc.perform(get("/myfile"))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
public void getPrivateFileTokenInHeader() throws Exception {
prepareMocksForPrivateFile();
.header("Authorization", "Bearer: <token>"))
.andDo(print())
.andExpect(status().isOk());
verify(gmsClient).isMemberOf(eq("<token>"), eq("group1"));
}
@Test
public void getPrivateFileTokenInQueryString() throws Exception {
prepareMocksForPrivateFile();
.andDo(print())
.andExpect(status().isOk());
verify(gmsClient).isMemberOf(eq("<token>"), eq("group1"));
}
private void prepareMocksForPrivateFile() {
Map<String, Object> claims = new HashMap<>();
claims.put("sub", "123");
when(tokenParser.getClaims(any())).thenReturn(claims);
when(gmsClient.isMemberOf(any(), any())).thenReturn(true);
FileInfo fileInfo = new FileInfo();
fileInfo.setOsRelPath(tempFile.getAbsolutePath());
fileInfo.setGroupRead(Collections.singletonList("group1"));
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
@Test
public void getPrivateFileByOwnerId() throws Exception {
Map<String, Object> claims = new HashMap<>();
claims.put("sub", "123");
when(tokenParser.getClaims(any())).thenReturn(claims);
FileInfo fileInfo = new FileInfo();
fileInfo.setOsRelPath(tempFile.getAbsolutePath());
fileInfo.setOwnerId("123");
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
mockMvc.perform(get("/path/to/myfile")
.header("Authorization", "Bearer: <token>"))
.andDo(print())
.andExpect(status().isOk());
}
@Test
public void testPrivateFileNullToken() throws Exception {
FileInfo fileInfo = new FileInfo();
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
mockMvc.perform(get("/path/to/myfile"))
.andDo(print())
.andExpect(status().isUnauthorized());
}