UNPKG

3.38 kBJavaScriptView Raw
1require('./common.js');
2
3var querystring = require('querystring');
4var crypto = require("crypto");
5
6describe("default LastFmNode instance")
7 before(function() { this.lastfm = new LastFmNode(); })
8
9 it("requests json", function() {
10 assert.equal('json', this.lastfm.params.format);
11 });
12
13 it("has default host", function() {
14 assert.equal('ws.audioscrobbler.com', this.lastfm.host);
15 });
16
17describe("LastFmNode requestUrl")
18 before(function() { this.lastfm = new LastFmNode(); })
19
20 it("appends stringified params to url", function() {
21 this.lastfm.params = { foo : "bar", baz : "bash" };
22 assert.equal("/2.0?foo=bar&baz=bash", this.lastfm.requestUrl());
23 });
24
25 it("appends additional params to url", function() {
26 this.lastfm.params = { foo : "bar", baz : "bash" };
27 additionalParams = { flip : "flop" };
28 assert.equal("/2.0?foo=bar&baz=bash&flip=flop", this.lastfm.requestUrl(additionalParams));
29 });
30
31 it("leaves original additional params untouched", function() {
32 this.lastfm.params = { foo: "bar" };
33 var url = this.lastfm.requestUrl({ foo: "baz" });
34 assert.equal("bar", this.lastfm.params.foo);
35 });
36
37describe("merge params")
38 before(function() { this.lastfm = new LastFmNode(); })
39
40 it("merges two param objects", function() {
41 var merged = this.lastfm.mergeParams({'foo': 'bar'}, {'bar': 'baz'});
42 assert.equal('bar', merged.foo);
43 assert.equal('baz', merged.bar);
44 });
45
46 it("second param takes precedence", function() {
47 var merged = this.lastfm.mergeParams({'foo': 'bar'}, {'foo': 'baz'});
48 assert.equal('baz', merged.foo);
49 });
50
51describe("LastFmNode signature hash")
52 // see http://www.last.fm/api/webauth#6
53 before(function() {
54 this.lastfm = new LastFmNode({ secret: 'secret' });
55 var that = this;
56 this.params = null;
57 this.whenParamsAre = function(params) {
58 that.params = params;
59 };
60
61 this.expectHashOf = function(unhashed) {
62 var expectedHash = crypto.createHash("md5").update(unhashed, "utf8").digest("hex");
63 that.expectHashToBe(expectedHash);
64 };
65
66 this.expectHashToBe = function(hash) {
67 var actualHash = that.lastfm.signature(that.params);
68 assert.equal(hash, actualHash);
69 }
70 })
71
72 it("includes params plus secret", function() {
73 this.whenParamsAre({ foo : "bar" });
74 this.expectHashOf("foobarsecret");
75 });
76
77 it("orders params alphabetically", function() {
78 this.whenParamsAre({ foo : "bar", baz: "bash", flip : "flop"});
79 this.expectHashOf("bazbashflipflopfoobarsecret");
80 });
81
82 it("ignores format parameter", function() {
83 this.whenParamsAre({ foo : "bar", baz : "bash", format: "json" });
84 this.expectHashOf("bazbashfoobarsecret");
85 });
86
87 it("handles high characters as expected by last.fm", function() {
88 this.whenParamsAre({ track: 'Tony’s Theme (Remastered)' });
89 this.expectHashOf("trackTony’s Theme (Remastered)secret");
90 this.expectHashToBe("9f92abf69e1532ec6e4686453c117688");
91 });
92
93describe("LastFmNode options")
94 before(function() {
95 this.options = {
96 api_key: 'abcdef12345',
97 secret: 'ghijk67890'
98 };
99 this.lastfm = new LastFmNode(this.options);
100 })
101
102 it("configures api key", function() {
103 assert.equal('abcdef12345', this.lastfm.params.api_key);
104 });
105
106 it("configures secret", function() {
107 assert.equal('ghijk67890', this.lastfm.secret);
108 });