Skip to content

Commit d3baa4b

Browse files
fix: tests and bugs
1 parent 99dc38c commit d3baa4b

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

plugin/src/main/java/io/snyk/eclipse/plugin/preferences/Preferences.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.snyk.eclipse.plugin.preferences;
22

3+
import static io.snyk.eclipse.plugin.EnvironmentConstants.ENV_SNYK_API;
4+
import static io.snyk.eclipse.plugin.EnvironmentConstants.ENV_SNYK_ORG;
35
import static io.snyk.eclipse.plugin.utils.FileSystemUtil.getBinaryDirectory;
6+
import static org.apache.commons.lang3.SystemUtils.getEnvironmentVariable;
47

58
import java.io.File;
69
import java.util.Arrays;
@@ -12,7 +15,6 @@
1215
import java.util.concurrent.CompletableFuture;
1316
import java.util.concurrent.ConcurrentHashMap;
1417

15-
import org.apache.commons.lang3.SystemUtils;
1618
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
1719
import org.eclipse.core.runtime.preferences.InstanceScope;
1820
import org.eclipse.equinox.security.storage.ISecurePreferences;
@@ -131,17 +133,21 @@ public static synchronized Preferences getTestInstance(IEclipsePreferences insec
131133
insecureStore.setDefault(DEVICE_ID, UUID.randomUUID().toString());
132134
insecureStore.setDefault(RELEASE_CHANNEL, "stable");
133135
insecureStore.setDefault(CLI_PATH, getDefaultCliPath());
136+
insecureStore.setDefault(ENDPOINT_KEY, DEFAULT_ENDPOINT);
137+
insecureStore.setDefault(ORGANIZATION_KEY, "");
134138

135-
String endpoint = SystemUtils.getEnvironmentVariable(EnvironmentConstants.ENV_SNYK_API, "");
136-
if (endpoint == null || endpoint.isBlank()) {
137-
endpoint = DEFAULT_ENDPOINT;
139+
var endpoint = getEnvironmentVariable(ENV_SNYK_API, "");
140+
if (endpoint != null && !endpoint.isBlank()) {
141+
store(ENDPOINT_KEY, endpoint);
138142
}
139-
insecureStore.setDefault(ENDPOINT_KEY, endpoint);
140-
insecureStore.setDefault(ORGANIZATION_KEY,
141-
SystemUtils.getEnvironmentVariable(EnvironmentConstants.ENV_SNYK_ORG, ""));
142-
143-
String token = SystemUtils.getEnvironmentVariable(EnvironmentConstants.ENV_SNYK_TOKEN, "");
144-
if (getPref(AUTH_TOKEN_KEY) == null && !"".equals(token)) {
143+
144+
var org = getEnvironmentVariable(ENV_SNYK_ORG, "");
145+
if (org != null && !org.isBlank()) {
146+
store(ORGANIZATION_KEY, org);
147+
}
148+
149+
String token = getEnvironmentVariable(EnvironmentConstants.ENV_SNYK_TOKEN, "");
150+
if (getPref(AUTH_TOKEN_KEY) != null && !"".equals(token)) {
145151
store(AUTH_TOKEN_KEY, token);
146152
}
147153

@@ -162,8 +168,8 @@ private void migratePreferences() {
162168
try {
163169
final var defaultString = insecureStore.getDefaultString(constant);
164170
final var value = securePreferences.get(constant, defaultString);
165-
if (insecureStore.isDefault(constant)) {
166-
this.insecurePreferences.put(constant, value);
171+
if (insecureStore.isDefault(constant) && !defaultString.equals(value)) {
172+
this.insecurePreferences.put(constant, value);
167173
}
168174
securePreferences.remove(constant);
169175
} catch (IllegalStateException | StorageException e) { // NOPMD // no handling needed in this case
@@ -201,7 +207,7 @@ private final String getDefaultCliPath() {
201207
}
202208

203209
public final String getPref(String key) {
204-
return getPref(key, null);
210+
return getPref(key, insecureStore.getDefaultString(key));
205211
}
206212

207213
public final String getPref(String key, String defaultValue) {
@@ -230,11 +236,11 @@ public final String getAuthToken() {
230236
}
231237

232238
public final String getEndpoint() {
233-
return getPref(ENDPOINT_KEY, DEFAULT_ENDPOINT);
239+
return getPref(ENDPOINT_KEY);
234240
}
235241

236242
public final String getLspVersion() {
237-
return getPref(LSP_VERSION);
243+
return insecurePreferences.get(LSP_VERSION, insecureStore.getDefaultString(LSP_VERSION));
238244
}
239245

240246
public final Optional<String> getPath() {
@@ -250,15 +256,15 @@ public final String getCliPath() {
250256
}
251257

252258
public final boolean isInsecure() {
253-
return insecurePreferences.getBoolean(INSECURE_KEY, false);
259+
return insecurePreferences.getBoolean(INSECURE_KEY, insecureStore.getDefaultBoolean(INSECURE_KEY));
254260
}
255261

256262
public final void setIsInsecure(boolean isInsecure) {
257263
insecurePreferences.put(INSECURE_KEY, Boolean.toString(isInsecure));
258264
}
259265

260266
public final boolean isManagedBinaries() {
261-
return insecurePreferences.getBoolean(MANAGE_BINARIES_AUTOMATICALLY, true);
267+
return insecurePreferences.getBoolean(MANAGE_BINARIES_AUTOMATICALLY, insecureStore.getDefaultBoolean(MANAGE_BINARIES_AUTOMATICALLY));
262268
}
263269

264270
public final void store(String key, String value) {
@@ -280,7 +286,7 @@ public final void store(String key, String value) {
280286
}
281287

282288
public final boolean getBooleanPref(String key) {
283-
return insecurePreferences.getBoolean(key, false);
289+
return insecurePreferences.getBoolean(key, insecureStore.getDefaultBoolean(key));
284290
}
285291

286292
public final boolean getBooleanPref(String key, boolean defaultValue) {
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package io.snyk.eclipse.plugin.preferences;
22

3+
import org.eclipse.jface.preference.IPreferenceStore;
34
import org.eclipse.jface.preference.StringFieldEditor;
45
import org.eclipse.swt.widgets.Composite;
56

6-
77
public class TokenFieldEditor extends StringFieldEditor {
8-
protected TokenFieldEditor(Preferences preferences, String name, String labelText,
9-
Composite parent) {
10-
super(name, labelText, 80, parent);
11-
setPreferenceStore(preferences.getSecureStore()); //NOPMD
12-
getTextControl().setEchoChar('*'); //NOPMD
13-
}
8+
private Preferences preferences;
149

15-
public void emptyTextfield() {
16-
setStringValue("");
17-
}
10+
protected TokenFieldEditor(Preferences preferences, String name, String labelText, Composite parent) {
11+
super(name, labelText, 80, parent);
12+
this.preferences = preferences;
13+
super.setPreferenceStore(preferences.getSecureStore()); // NOPMD
14+
getTextControl().setEchoChar('*'); // NOPMD
15+
}
1816

17+
public void emptyTextfield() {
18+
setStringValue("");
19+
}
1920

21+
@Override
22+
public void setPreferenceStore(IPreferenceStore store) {
23+
// we don't let the page override the preference store to a non-secure store
24+
super.setPreferenceStore(preferences.getSecureStore());
25+
}
2026
}

tests/src/test/java/io/snyk/eclipse/plugin/properties/preferences/PreferencesTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ void test_DefaultPreferences() {
6565
assertEquals("true", preferences.getPref(FILTER_IGNORES_SHOW_OPEN_ISSUES));
6666
assertEquals("false", preferences.getPref(FILTER_IGNORES_SHOW_IGNORED_ISSUES));
6767
assertEquals("false", preferences.getPref(FILTER_SHOW_ONLY_FIXABLE));
68-
assertEquals("true", preferences.getPref(SEND_ERROR_REPORTS));
69-
assertEquals("true", preferences.getPref(ENABLE_TELEMETRY));
7068
assertEquals("true", preferences.getPref(MANAGE_BINARIES_AUTOMATICALLY));
7169
assertEquals("1", preferences.getPref(LSP_VERSION));
7270
assertEquals("false", preferences.getPref(IS_GLOBAL_IGNORES_FEATURE_ENABLED));
@@ -86,8 +84,9 @@ void test_ExistingTokenInEnvironment_IsStoredInPreferences() {
8684
.thenReturn("token");
8785

8886
Preferences prefs = Preferences.getTestInstance(new InMemoryPreferenceStore(), new InMemorySecurePreferenceStore());
87+
prefs.waitForSecureStorage();
8988

90-
assertEquals(prefs.getAuthToken(), "token");
89+
assertEquals("token", prefs.getAuthToken());
9190
}
9291
}
9392

@@ -111,7 +110,7 @@ void test_ExistingOrgInEnvironment_IsStoredInPreferences() {
111110

112111
Preferences prefs = Preferences.getTestInstance(new InMemoryPreferenceStore(), new InMemorySecurePreferenceStore());
113112

114-
assertEquals(prefs.getPref(Preferences.ORGANIZATION_KEY), "myOrg");
113+
assertEquals("myOrg",prefs.getPref(Preferences.ORGANIZATION_KEY));
115114
}
116115
}
117116

0 commit comments

Comments
 (0)