Skip to content

Commit 831bf99

Browse files
authored
Merge pull request #63 from GitGuardian/salomevoltz/fix-refresh-quota-button
fix(refresh-quota): Fix button refresh quota
2 parents 8b89858 + 07f604d commit 831bf99

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

src/extension.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ export function activate(context: ExtensionContext) {
114114

115115
//generic commands to open correct view on status bar click
116116
registerOpenViewsCommands(context, outputChannel);
117-
commands.registerCommand(
118-
"gitguardian.refreshQuota",
119-
ggshieldQuotaViewProvider.refresh
117+
commands.registerCommand("gitguardian.refreshQuota", () =>
118+
ggshieldQuotaViewProvider.refresh()
120119
);
121120

122121
context.subscriptions.push(

src/ggshield-webview/gitguardian-quota-webview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class GitGuardianQuotaWebviewProvider
5858
<p>Loading...</p>
5959
</body>
6060
</html>`;
61-
} else if (this.quota !== undefined) {
61+
} else if (this.quota !== 0 && this.isAuthenticated) {
6262
computedHtml = `
6363
<!DOCTYPE html>
6464
<html lang="en">
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import * as assert from "assert";
2+
import { GitGuardianQuotaWebviewProvider } from "../../../ggshield-webview/gitguardian-quota-webview";
3+
import { GGShieldConfiguration } from "../../../lib/ggshield-configuration";
4+
import {
5+
ExtensionContext,
6+
Memento,
7+
Uri,
8+
WebviewOptions,
9+
WebviewView,
10+
} from "vscode";
11+
12+
suite("GitGuardianQuotaWebviewProvider", () => {
13+
let provider: GitGuardianQuotaWebviewProvider;
14+
let mockGGShieldConfig: Partial<GGShieldConfiguration>;
15+
let mockWebviewView: Partial<WebviewView>;
16+
let mockWorkspaceState: Memento & {
17+
setKeysForSync(keys: readonly string[]): void;
18+
};
19+
let mockContext: Partial<ExtensionContext>;
20+
21+
setup(() => {
22+
mockWorkspaceState = {
23+
get: (key: string) => undefined,
24+
update: (key: string, value: any) => {
25+
key = value;
26+
return Promise.resolve();
27+
},
28+
keys: () => [],
29+
setKeysForSync: (keys: readonly string[]) => {},
30+
};
31+
32+
mockContext = {
33+
workspaceState: mockWorkspaceState,
34+
};
35+
36+
provider = new GitGuardianQuotaWebviewProvider(
37+
{} as GGShieldConfiguration,
38+
Uri.parse("file:///mock"),
39+
mockContext as ExtensionContext
40+
);
41+
42+
mockWebviewView = {
43+
webview: {
44+
html: "",
45+
onDidReceiveMessage: () => ({ dispose: () => {} }),
46+
cspSource: "",
47+
options: {} as WebviewOptions,
48+
postMessage: (message: any) => Promise.resolve(true),
49+
asWebviewUri: (uri: Uri) => uri,
50+
},
51+
onDidChangeVisibility: () => ({ dispose: () => {} }),
52+
visible: false,
53+
};
54+
provider["_view"] = mockWebviewView as WebviewView;
55+
});
56+
57+
test("should update the webview content when loading", () => {
58+
provider["isLoading"] = true;
59+
provider["updateWebViewContent"]();
60+
61+
assert.ok(provider["_view"]?.webview.html.includes("<p>Loading...</p>"));
62+
});
63+
64+
test("should display the quota when authenticated", () => {
65+
provider["isLoading"] = false;
66+
provider["isAuthenticated"] = true;
67+
provider["quota"] = 100;
68+
69+
provider["updateWebViewContent"]();
70+
71+
assert.ok(
72+
provider["_view"]?.webview.html.includes("<p>Your current quota: 100</p>")
73+
);
74+
});
75+
76+
test("should display authentication prompt when unauthenticated", () => {
77+
provider["isLoading"] = false;
78+
provider["isAuthenticated"] = false;
79+
80+
provider["updateWebViewContent"]();
81+
82+
assert.ok(
83+
provider["_view"]?.webview.html.includes(
84+
"<p>Please authenticate to see your quota.</p>"
85+
)
86+
);
87+
});
88+
});

0 commit comments

Comments
 (0)