1 | require('./common.js');
|
2 | var LastFmSession = require('lastfm/lastfm-session');
|
3 | var 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 = 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 authorisation not successful", function() {
|
129 | whenReadRequestReturns(FakeData.AuthorisationError);
|
130 | andTokenIs("token");
|
131 | expectError("Signature is invalid");
|
132 | });
|
133 |
|
134 | it("emits error when receiving unexpected return data", function() {
|
135 | whenReadRequestReturns(FakeData.SingleRecentTrack);
|
136 | andTokenIs("token");
|
137 | expectError("Unexpected error");
|
138 | });
|
139 |
|
140 | it("emits authorised when successful", function() {
|
141 | whenReadRequestReturns(FakeData.SuccessfulAuthorisation);
|
142 | andTokenIs("token");
|
143 | expectAuthorisation();
|
144 | });
|
145 |
|
146 | it("can have error handler specified with authorise call", function() {
|
147 | var handler = { error: function(error) { } };
|
148 | gently.expect(handler, "error", function(error) {
|
149 | assert.equal("No token supplied", error.message);
|
150 | });
|
151 | session.authorise("", { handlers: {
|
152 | error: handler.error
|
153 | }});
|
154 | });
|
155 |
|
156 | it("updates session key and user when successful", function() {
|
157 | whenReadRequestReturns(FakeData.SuccessfulAuthorisation);
|
158 | andTokenIs("token");
|
159 | expectAuthorisation(function(session) {
|
160 | assert.equal("username", session.user);
|
161 | assert.equal("sessionkey", session.key);
|
162 | assert.ok(session.isAuthorised());
|
163 | });
|
164 | });
|
165 |
|
166 | it("can have authorised handler specified with authorise call", function() {
|
167 | var handler = { authorised: function(session) { } };
|
168 | whenReadRequestReturns(FakeData.SuccessfulAuthorisation);
|
169 | session.authorise("token", { handlers: {
|
170 | authorised: handler.authorised
|
171 | }});
|
172 | });
|
173 |
|
174 | it("bubbles up errors", function() {
|
175 | var errorMessage = "Bubbled error";
|
176 | whenReadRequestThrowsError(errorMessage);
|
177 | andTokenIs("token");
|
178 | expectError(errorMessage);
|
179 | });
|
180 | })();
|