UNPKG

4.78 kBJavaScriptView Raw
1require('./common.js');
2var LastFmSession = require('../lib/lastfm/lastfm-session');
3var fakes = require("./fakes");
4
5(function() {
6 describe("a new LastFmSession");
7 var session;
8
9 before(function() {
10 session = new LastFmSession(new LastFmNode());
11 });
12
13 it("has no session key", function() {
14 assert.ok(!session.key);
15 });
16
17 it("has no user", function() {
18 assert.ok(!session.user);
19 });
20
21 it("is not authorised", function() {
22 assert.ok(!session.isAuthorised());
23 });
24
25 it("can configure key and user", function() {
26 var session = new LastFmSession(new LastFmNode(), "user", "sessionkey");
27 assert.equal("user", session.user);
28 assert.equal("sessionkey", session.key);
29 });
30
31 it("is authorised when it has a key", function() {
32 var session = new LastFmSession(new LastFmNode(), "user", "sessionkey");
33 assert.ok(session.isAuthorised());
34 });
35})();
36
37(function() {
38 var readError, token, returndata, options, request, lastfm, session, gently;
39
40 function setupFixture() {
41 readError = "";
42 token = "";
43 returndata = null;
44 options = null;
45 request = new fakes.LastFmRequest();
46 lastfm = new LastFmNode();
47 session = new LastFmSession(lastfm);
48 gently = new Gently();
49 }
50
51 function expectError(expectedError) {
52 gently.expect(session, "emit", function(event, error) {
53 assert.equal("error", event);
54 assert.equal(expectedError, error.message);
55 });
56 session.authorise(token, options);
57 if (readError) {
58 request.emit("error", new Error(readError));
59 }
60 else {
61 request.emit("success", returndata);
62 }
63 }
64
65 function expectAuthorisation(assertions) {
66 gently.expect(session, "emit", function(event, emittedSession) {
67 assert.equal("authorised", event);
68 if (assertions) {
69 assertions(emittedSession);
70 }
71 });
72 session.authorise(token, options);
73 request.emit("success", returndata);
74 }
75
76 function whenReadRequestReturns(data) {
77 returndata = JSON.parse(data);
78 gently.expect(lastfm, "request", function() {
79 return request;
80 });
81 }
82
83 function whenReadRequestThrowsError(message) {
84 readError = message;
85 gently.expect(lastfm, "request", function() {
86 return request;
87 });
88 }
89
90 function andTokenIs(setToken) {
91 token = setToken;
92 }
93
94 function andOptionsAre(setOptions) {
95 options = setOptions;
96 }
97
98 describe("a LastFmSession authorisation request")
99 before(function() {
100 setupFixture();
101 });
102
103 it("emits error when no token supplied", function() {
104 expectError("No token supplied");
105 });
106
107 it("contains supplied token", function() {
108 gently.expect(lastfm, "request", function(method, params) {
109 assert.equal("token", params.token);
110 return request;
111 });
112 session.authorise("token");
113 });
114
115 it("uses getSession method", function() {
116 gently.expect(lastfm, "request", function(method, params) {
117 assert.equal("auth.getsession", method);
118 return request;
119 });
120 session.authorise("token");
121 });
122
123 describe("a completed LastFmSession authorisation request")
124 before(function() {
125 setupFixture();
126 });
127
128 it("emits error when receiving unexpected return data", function() {
129 whenReadRequestReturns(FakeData.SingleRecentTrack);
130 andTokenIs("token");
131 expectError("Unexpected error");
132 });
133
134 it("emits authorised when successful", function() {
135 whenReadRequestReturns(FakeData.SuccessfulAuthorisation);
136 andTokenIs("token");
137 expectAuthorisation();
138 });
139
140 it("can have error handler specified with authorise call", function() {
141 var handler = { error: function(error) { } };
142 gently.expect(handler, "error", function(error) {
143 assert.equal("No token supplied", error.message);
144 });
145 session.authorise("", { handlers: {
146 error: handler.error
147 }});
148 });
149
150 it("updates session key and user when successful", function() {
151 whenReadRequestReturns(FakeData.SuccessfulAuthorisation);
152 andTokenIs("token");
153 expectAuthorisation(function(session) {
154 assert.equal("username", session.user);
155 assert.equal("sessionkey", session.key);
156 assert.ok(session.isAuthorised());
157 });
158 });
159
160 it("can have authorised handler specified with authorise call", function() {
161 var handler = { authorised: function(session) { } };
162 whenReadRequestReturns(FakeData.SuccessfulAuthorisation);
163 gently.expect(handler, "authorised");
164 session.authorise("token", { handlers: {
165 authorised: handler.authorised
166 }});
167 request.emit("success", returndata);
168 });
169
170 it("bubbles up errors", function() {
171 var errorMessage = "Bubbled error";
172 whenReadRequestThrowsError(errorMessage);
173 andTokenIs("token");
174 expectError(errorMessage);
175 });
176})();