-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathfast.js
More file actions
122 lines (102 loc) · 3.44 KB
/
Copy pathfast.js
File metadata and controls
122 lines (102 loc) · 3.44 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
import { jest } from "@jest/globals";
import { tick } from "@xmpp/events";
import { mockClient, xml } from "@xmpp/test";
import { datetime } from "@xmpp/time";
import { Element } from "@xmpp/xml";
const mechanism = "HT-SHA-256-NONE";
test("requests and saves token if server advertises fast", async () => {
const { entity, fast } = mockClient();
const spy_saveToken = jest.spyOn(fast, "saveToken");
entity.mockInput(
// prettier-ignore
xml('features', { xmlns: "http://etherx.jabber.org/streams" },
xml('authentication', { xmlns: "urn:xmpp:sasl:2" },
xml('mechanism', {}, "PLAIN"),
xml('inline', {},
xml('fast', { xmlns: "urn:xmpp:fast:0" },
xml('mechanism', {}, mechanism)
)
)
)
),
);
const authenticate = await entity.catchOutgoing();
expect(authenticate.is("authenticate", "urn:xmpp:sasl:2")).toBe(true);
const request_token = authenticate.getChild(
"request-token",
"urn:xmpp:fast:0",
);
expect(request_token.attrs.mechanism).toBe(mechanism);
const token = "secret-token:fast-HZzFpFwHTy4nc3C8Y1NVNZqYef_7Q3YjMLu2";
const expiry = "2025-02-06T09:58:40.774329Z";
expect(spy_saveToken).not.toHaveBeenCalled();
entity.mockInput(
// prettier-ignore
xml('success', { xmlns: "urn:xmpp:sasl:2" },
xml('token', { expiry, xmlns: "urn:xmpp:fast:0" , token}),
xml('authorization-identifier', {}, "username@localhost/rOYwkWIywtnF")
),
);
expect(spy_saveToken).toHaveBeenCalledWith({ token, expiry, mechanism });
});
async function setupFast() {
const { entity, fast } = mockClient();
const d = new Date();
d.setFullYear(d.getFullYear() + 1);
const expiry = datetime(d);
fast.fetchToken = async () => {
return {
mechanism,
expiry,
token: "foobar",
};
};
entity.mockInput(
// prettier-ignore
xml('features', { xmlns: "http://etherx.jabber.org/streams" },
xml('authentication', { xmlns: "urn:xmpp:sasl:2" },
xml('mechanism', {}, "PLAIN"),
xml('inline', {},
xml('fast', { xmlns: "urn:xmpp:fast:0" },
xml('mechanism', {}, mechanism)
)
)
)
),
);
expect(fast.mechanism).toBe(mechanism);
const authenticate = await entity.catchOutgoing();
expect(authenticate.is("authenticate", "urn:xmpp:sasl:2"));
expect(authenticate.attrs.mechanism).toBe(mechanism);
expect(authenticate.getChild("fast", "urn:xmpp:fast:0")).toBeInstanceOf(
Element,
);
return entity;
}
test("deletes the token if server replies with not-authorized", async () => {
const entity = await setupFast();
const spy_deleteToken = jest.spyOn(entity.fast, "deleteToken");
expect(spy_deleteToken).not.toHaveBeenCalled();
entity.mockInput(
// prettier-ignore
xml("failure", { xmlns: "urn:xmpp:sasl:2" },
xml("not-authorized", { xmlns: "urn:ietf:params:xml:ns:xmpp-sasl" }),
),
);
await tick();
expect(spy_deleteToken).toHaveBeenCalled();
});
test("deletes the token if server replies with credentials-expired", async () => {
const entity = await setupFast();
const spy_deleteToken = jest.spyOn(entity.fast, "deleteToken");
// credentials-expired
expect(spy_deleteToken).not.toHaveBeenCalled();
entity.mockInput(
// prettier-ignore
xml("failure", { xmlns: "urn:xmpp:sasl:2" },
xml("credentials-expired", { xmlns: "urn:ietf:params:xml:ns:xmpp-sasl" }),
),
);
await tick();
expect(spy_deleteToken).toHaveBeenCalled();
});