Repository navigation
Expand file tree
/
Copy pathNoteControllerShowTests.java
More file actions
137 lines (114 loc) · 6.07 KB
/
Copy pathNoteControllerShowTests.java
File metadata and controls
137 lines (114 loc) · 6.07 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
package com.odde.donut.controllers;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.odde.donut.controllers.dto.NoteRealm;
import com.odde.donut.controllers.dto.NoteUpdateContentDTO;
import com.odde.donut.entities.Folder;
import com.odde.donut.entities.Note;
import com.odde.donut.entities.repositories.NoteRepository;
import com.odde.donut.exceptions.UnexpectedNoAccessRightException;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
class NoteControllerShowTests extends ControllerTestBase {
@Autowired NoteController controller;
@Autowired TextContentController textContentController;
@Autowired NoteRepository noteRepository;
@Autowired EntityManager entityManager;
@BeforeEach
void setup() {
currentUser.setUser(makeMe.aUser().please());
}
private void authorWikiLinkTo(Note carrier, String targetTitle)
throws UnexpectedNoAccessRightException {
NoteUpdateContentDTO contentDto = new NoteUpdateContentDTO();
contentDto.setContent("See [[" + targetTitle + "]].");
textContentController.updateNoteContent(carrier, contentDto);
}
@Test
void shouldNotBeAbleToSeeNoteIDontHaveAccessTo() {
Note note = makeMe.aNote().notebookOwnedBy(makeMe.aUser().please()).please();
assertThrows(UnexpectedNoAccessRightException.class, () -> controller.showNote(note));
}
@Test
void shouldBeAbleToSeeOwnNote() throws UnexpectedNoAccessRightException {
Note note = makeMe.aNote().notebookOwnedBy(currentUser.getUser()).please();
NoteRealm noteRealm = controller.showNote(note);
assertThat(noteRealm.getId(), equalTo(note.getId()));
assertThat(noteRealm.sidebar().getNotebookRealm().readonly(), is(false));
assertThat(
noteRealm.sidebar().getNotebookRealm().notebook().getId(),
equalTo(note.getNotebook().getId()));
}
@Test
void shouldReturnReadonlyWhenHavingReadingAuthOnly() throws UnexpectedNoAccessRightException {
Note note = makeMe.aNote().notebookOwnedBy(makeMe.aUser().please()).please();
makeMe.aBazaarNotebook(note.getNotebook()).please();
assertThat(controller.showNote(note).sidebar().getNotebookRealm().readonly(), is(true));
}
@Test
void rootRelativeNoteUrl_appearsAsInboundReferenceOnTarget()
throws UnexpectedNoAccessRightException {
Note target =
makeMe.aNote().notebookOwnedBy(currentUser.getUser()).title("Url Target").please();
Note source = makeMe.aNote().underSameNotebookAs(target).please();
NoteUpdateContentDTO contentDto = new NoteUpdateContentDTO();
contentDto.setContent("[any display](/n" + target.getId() + ")");
textContentController.updateNoteContent(source, contentDto);
NoteRealm targetRealm = controller.showNote(target);
assertThat(targetRealm.getReferences(), hasSize(1));
assertThat(targetRealm.getReferences().getFirst().getId(), equalTo(source.getId()));
}
@Test
void newlyCreatedTargetImmediatelyGainsCurrentInboundReferencesWithoutRefreshingSource()
throws UnexpectedNoAccessRightException {
Note source = makeMe.aNote().notebookOwnedBy(currentUser.getUser()).please();
authorWikiLinkTo(source, "Future");
// "Future" does not exist yet: the authored wiki link is missing, so it resolves to nothing.
assertThat(controller.showNote(source).getWikiLinks(), hasSize(0));
Note future = makeMe.aNote().underSameNotebookAs(source).title("Future").please();
// Showing "Future" — without any refresh/re-save of "source" — already lists it as inbound.
NoteRealm futureRealm = controller.showNote(future);
assertThat(futureRealm.getReferences(), hasSize(1));
assertThat(futureRealm.getReferences().getFirst().getId(), equalTo(source.getId()));
}
@Test
void namesakeCreationRemovesNowAmbiguousInboundReferenceFromBothTargets()
throws UnexpectedNoAccessRightException {
Folder homeFolder = makeMe.aFolder().notebookOwnedBy(currentUser.getUser()).please();
Note target = makeMe.aNote().title("Target").folder(homeFolder).please();
Note source = makeMe.aNote().underSameNotebookAs(target).please();
authorWikiLinkTo(source, "Target");
// Unique "Target" resolves: source appears inbound.
assertThat(controller.showNote(target).getReferences(), hasSize(1));
assertThat(
controller.showNote(target).getReferences().getFirst().getId(), equalTo(source.getId()));
// A namesake in a different folder of the same notebook makes "[[Target]]" ambiguous between
// the two same-titled notes (a plain, non-path-shaped token doesn't pin a folder).
Folder otherFolder = makeMe.aFolder().notebook(homeFolder.getNotebook()).please();
Note namesake = makeMe.aNote().title("Target").folder(otherFolder).please();
// Neither same-named note lists source as inbound any more.
assertThat(controller.showNote(target).getReferences(), hasSize(0));
assertThat(controller.showNote(namesake).getReferences(), hasSize(0));
// The source's own authored content/entry is unchanged (still the same authored token).
NoteRealm sourceRealm = controller.showNote(source);
assertThat(sourceRealm.getId(), equalTo(source.getId()));
}
@Test
void deeplyTrashedReferrerDisappearsFromTargetAfterFreshReload()
throws UnexpectedNoAccessRightException {
Note target = makeMe.aNote().notebookOwnedBy(currentUser.getUser()).title("A").please();
Folder depthOne = makeMe.aFolder().notebook(target.getNotebook()).name("Depth One").please();
Folder depthTwo = makeMe.aFolder().parentFolder(depthOne).name("Depth Two").please();
Note referrer = makeMe.aNote().folder(depthTwo).title("B").please();
authorWikiLinkTo(referrer, "A");
controller.trashNote(referrer, leaveDeadLinks());
entityManager.flush();
entityManager.clear();
Note reloadedTarget = noteRepository.findById(target.getId()).orElseThrow();
NoteRealm targetRealm = controller.showNote(reloadedTarget);
assertThat(targetRealm.getReferences(), hasSize(0));
}
}