Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 9dd1a52

Browse files
committed
Better unit tests
From GitbookIO#100
1 parent 92fdc3a commit 9dd1a52

File tree

15 files changed

+638
-432
lines changed

15 files changed

+638
-432
lines changed

__tests__/download.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var request = require("supertest")
2+
var app = require("./testing").app
3+
4+
var MAC_USERAGENT =
5+
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us)" +
6+
" AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19"
7+
var WIN_USERAGENT =
8+
"Mozilla/5.0 (Windows NT 6.3; Trident/7.0; Touch; rv:11.0) like Gecko"
9+
10+
describe("Download", function () {
11+
var agent = request.agent(app)
12+
13+
describe("Latest version (/)", function () {
14+
test("should fail if no user-agent to detect platform", function (done) {
15+
agent.get("/").expect(400, done)
16+
})
17+
18+
test("should download windows file", function (done) {
19+
agent
20+
.get("/")
21+
.set("User-Agent", WIN_USERAGENT)
22+
.expect(200)
23+
.expect("Content-Disposition", "attachment; filename=test.exe")
24+
.expect("Content-Length", "13")
25+
.end(done)
26+
})
27+
28+
test("should download OS X file as DMG", function (done) {
29+
agent
30+
.get("/")
31+
.set("User-Agent", MAC_USERAGENT)
32+
.expect(200)
33+
.expect("Content-Disposition", "attachment; filename=test-osx.dmg")
34+
.expect("Content-Length", "19")
35+
.end(done)
36+
})
37+
})
38+
39+
describe("Previous version (/download/version/)", function () {
40+
test("should not have a windows file to download", function (done) {
41+
agent
42+
.get("/download/version/0.9.0")
43+
.set("User-Agent", WIN_USERAGENT)
44+
.expect(404)
45+
.end(done)
46+
})
47+
48+
test("should download OS X file as DMG", function (done) {
49+
agent
50+
.get("/download/version/0.9.0")
51+
.set("User-Agent", MAC_USERAGENT)
52+
.expect(200)
53+
.expect("Content-Disposition", "attachment; filename=test-osx.dmg")
54+
.expect("Content-Length", "19")
55+
.end(done)
56+
})
57+
})
58+
})

__tests__/platforms.js

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
var platforms = require("../lib/utils/platforms")
2+
var useragent = require("express-useragent")
3+
4+
describe("Platforms", function () {
5+
describe("Detect", function () {
6+
test("should detect osx_64", function () {
7+
expect(platforms.detect("myapp-v0.25.1-darwin-x64.zip")).toEqual(
8+
platforms.OSX_64,
9+
)
10+
expect(platforms.detect("myapp.dmg")).toEqual(platforms.OSX_64)
11+
})
12+
13+
test("should detect windows_32", function () {
14+
expect(platforms.detect("myapp-v0.25.1-win32-ia32.zip")).toEqual(
15+
platforms.WINDOWS_32,
16+
)
17+
expect(platforms.detect("atom-1.0.9-delta.nupkg")).toEqual(
18+
platforms.WINDOWS_32,
19+
)
20+
expect(platforms.detect("RELEASES")).toEqual(platforms.WINDOWS_32)
21+
})
22+
23+
test("should detect windows_64", function () {
24+
expect(platforms.detect("MyApp-x64.exe")).toEqual(platforms.WINDOWS_64)
25+
var chrome = useragent.parse(
26+
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
27+
)
28+
29+
expect(platforms.detectPlatformByUserAgent(chrome)).toEqual(
30+
platforms.WINDOWS_64,
31+
)
32+
var edge = useragent.parse(
33+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
34+
)
35+
expect(platforms.detectPlatformByUserAgent(edge)).toEqual(
36+
platforms.WINDOWS_64,
37+
)
38+
})
39+
40+
test("should detect linux", function () {
41+
expect(platforms.detect("enterprise-amd64.tar.gz")).toEqual(
42+
platforms.LINUX_64,
43+
)
44+
expect(platforms.detect("enterprise-amd64.tgz")).toEqual(
45+
platforms.LINUX_64,
46+
)
47+
expect(platforms.detect("enterprise-ia32.tar.gz")).toEqual(
48+
platforms.LINUX_32,
49+
)
50+
expect(platforms.detect("enterprise-ia32.tgz")).toEqual(
51+
platforms.LINUX_32,
52+
)
53+
})
54+
55+
test("should detect debian_32", function () {
56+
expect(platforms.detect("atom-ia32.deb")).toEqual(platforms.LINUX_DEB_32)
57+
})
58+
59+
test("should detect debian_64", function () {
60+
expect(platforms.detect("atom-amd64.deb")).toEqual(platforms.LINUX_DEB_64)
61+
})
62+
63+
test("should detect rpm_32", function () {
64+
expect(platforms.detect("atom-ia32.rpm")).toEqual(platforms.LINUX_RPM_32)
65+
})
66+
67+
test("should detect rpm_64", function () {
68+
expect(platforms.detect("atom-amd64.rpm")).toEqual(platforms.LINUX_RPM_64)
69+
})
70+
71+
describe("Resolve", function () {
72+
var version = {
73+
platforms: [
74+
{
75+
type: "osx_64",
76+
filename: "test-3.3.1-darwin.dmg",
77+
download_url:
78+
"https://api.github.com/repos/test/test2/releases/assets/793838",
79+
download_count: 2,
80+
},
81+
{
82+
type: "osx_64",
83+
filename: "test-3.3.1-darwin-x64.zip",
84+
download_url:
85+
"https://api.github.com/repos/test/test2/releases/assets/793869",
86+
download_count: 0,
87+
},
88+
{
89+
type: "windows_32",
90+
filename: "atom-1.0.9-delta.nupkg",
91+
size: 1457531,
92+
content_type: "application/zip",
93+
download_url:
94+
"https://api.github.com/repos/atom/atom/releases/assets/825732",
95+
download_count: 55844,
96+
},
97+
{
98+
type: "windows_32",
99+
filename: "atom-1.0.9-full.nupkg",
100+
size: 78181725,
101+
content_type: "application/zip",
102+
download_url:
103+
"https://api.github.com/repos/atom/atom/releases/assets/825730",
104+
download_count: 26987,
105+
},
106+
{
107+
type: "linux_32",
108+
filename: "atom-ia32.tar.gz",
109+
size: 71292506,
110+
content_type: "application/zip",
111+
download_url:
112+
"https://api.github.com/repos/atom/atom/releases/assets/825658",
113+
download_count: 2494,
114+
},
115+
{
116+
type: "linux_64",
117+
filename: "atom-amd64.tar.gz",
118+
size: 71292506,
119+
content_type: "application/zip",
120+
download_url:
121+
"https://api.github.com/repos/atom/atom/releases/assets/825658",
122+
download_count: 2494,
123+
},
124+
{
125+
type: "linux_rpm_32",
126+
filename: "atom-ia32.rpm",
127+
size: 71292506,
128+
content_type: "application/zip",
129+
download_url:
130+
"https://api.github.com/repos/atom/atom/releases/assets/825658",
131+
download_count: 2494,
132+
},
133+
{
134+
type: "linux_rpm_64",
135+
filename: "atom-amd64.rpm",
136+
size: 71292506,
137+
content_type: "application/zip",
138+
download_url:
139+
"https://api.github.com/repos/atom/atom/releases/assets/825658",
140+
download_count: 2494,
141+
},
142+
{
143+
type: "linux_deb_32",
144+
filename: "atom-ia32.deb",
145+
size: 71292506,
146+
content_type: "application/zip",
147+
download_url:
148+
"https://api.github.com/repos/atom/atom/releases/assets/825658",
149+
download_count: 2494,
150+
},
151+
{
152+
type: "linux_deb_64",
153+
filename: "atom-amd64.deb",
154+
size: 71292506,
155+
content_type: "application/zip",
156+
download_url:
157+
"https://api.github.com/repos/atom/atom/releases/assets/825658",
158+
download_count: 2494,
159+
},
160+
{
161+
type: "windows_32",
162+
filename: "atom-windows.zip",
163+
size: 79815714,
164+
content_type: "application/zip",
165+
download_url:
166+
"https://api.github.com/repos/atom/atom/releases/assets/825729",
167+
download_count: 463,
168+
},
169+
{
170+
type: "windows_32",
171+
filename: "AtomSetup.exe",
172+
size: 78675720,
173+
content_type: "application/zip",
174+
download_url:
175+
"https://api.github.com/repos/atom/atom/releases/assets/825728",
176+
download_count: 5612,
177+
},
178+
],
179+
}
180+
181+
test("should resolve to best platform", function () {
182+
expect(platforms.resolve(version, "osx").filename).toEqual(
183+
"test-3.3.1-darwin.dmg",
184+
)
185+
expect(platforms.resolve(version, "win32").filename).toEqual(
186+
"AtomSetup.exe",
187+
)
188+
expect(platforms.resolve(version, "linux_64").filename).toEqual(
189+
"atom-amd64.tar.gz",
190+
)
191+
expect(platforms.resolve(version, "linux_32").filename).toEqual(
192+
"atom-ia32.tar.gz",
193+
)
194+
expect(platforms.resolve(version, "linux_rpm_32").filename).toEqual(
195+
"atom-ia32.rpm",
196+
)
197+
expect(platforms.resolve(version, "linux_rpm_64").filename).toEqual(
198+
"atom-amd64.rpm",
199+
)
200+
expect(platforms.resolve(version, "linux_deb_32").filename).toEqual(
201+
"atom-ia32.deb",
202+
)
203+
expect(platforms.resolve(version, "linux_deb_64").filename).toEqual(
204+
"atom-amd64.deb",
205+
)
206+
})
207+
208+
test("should resolve to best platform with a preferred filetype", function () {
209+
expect(
210+
platforms.resolve(version, "osx", {
211+
filePreference: [".zip"],
212+
}).filename,
213+
).toEqual("test-3.3.1-darwin-x64.zip")
214+
})
215+
216+
test("should resolve to best platform with a wanted filetype", function () {
217+
expect(
218+
platforms.resolve(version, "osx", {
219+
wanted: ".zip",
220+
}).filename,
221+
).toEqual("test-3.3.1-darwin-x64.zip")
222+
})
223+
})
224+
})
225+
})

__tests__/testing.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var nuts = require("../lib")
2+
require("dotenv").config()
3+
4+
var config = {
5+
repository: "SamyPesse/nuts-testing",
6+
token: process.env.GITHUB_TOKEN,
7+
}
8+
9+
var instance = nuts.Nuts(config)
10+
var app = nuts.createApp(config)
11+
12+
module.exports = {
13+
app: app,
14+
nuts: instance,
15+
}
16+
17+
test("testing.js", () => {
18+
// this is a blank test, need to refactor this file
19+
expect(1).toEqual(1)
20+
})

__tests__/update.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var request = require("supertest")
2+
var expect = require("expect")
3+
4+
var app = require("./testing").app
5+
6+
describe("Update", function () {
7+
var agent = request.agent(app)
8+
9+
describe("Squirrel.Mac (OS X)", function () {
10+
describe("/update/osx/", function () {
11+
test("should return a 204 if using latest version", function (done) {
12+
agent.get("/update/osx/1.0.0").expect(204, done)
13+
})
14+
15+
test("should return a 200 with json if using old stable version", function (done) {
16+
agent
17+
.get("/update/osx/0.9.0")
18+
.expect("Content-Type", /json/)
19+
.expect(function (res) {
20+
expect(res.body.name).toBe("1.0.0")
21+
expect(res.body.url).toBeTruthy()
22+
expect(res.body.pub_date).toBeTruthy()
23+
})
24+
.expect(200, done)
25+
})
26+
27+
test("should return a 200 with json if using old beta version (1)", function (done) {
28+
agent
29+
.get("/update/osx/0.9.1-beta")
30+
.expect("Content-Type", /json/)
31+
.expect(function (res) {
32+
expect(res.body.name).toBe("1.0.0")
33+
})
34+
.expect(200, done)
35+
})
36+
37+
test("should return a 200 with json if using old beta version (2)", function (done) {
38+
agent
39+
.get("/update/osx/0.9.1-beta.1")
40+
.expect("Content-Type", /json/)
41+
.expect(function (res) {
42+
expect(res.body.name).toBe("1.0.0")
43+
})
44+
.expect(200, done)
45+
})
46+
47+
test("should return a 200 with json if using old alpha version (1)", function (done) {
48+
agent
49+
.get("/update/osx/0.9.2-alpha.2")
50+
.expect("Content-Type", /json/)
51+
.expect(function (res) {
52+
expect(res.body.name).toBe("1.0.0")
53+
})
54+
.expect(200, done)
55+
})
56+
})
57+
58+
describe("/update/channel/beta/osx", function () {
59+
test("should update from 0.9.1-beta to 1.0.1-beta.0", function (done) {
60+
agent
61+
.get("/update/channel/beta/osx/0.9.1-beta")
62+
.expect("Content-Type", /json/)
63+
.expect(function (res) {
64+
expect(res.body.name).toBe("1.0.1-beta.0")
65+
})
66+
.expect(200, done)
67+
})
68+
69+
test("should not update from 1.0.1-beta.0", function (done) {
70+
agent.get("/update/channel/beta/osx/1.0.1-beta.0").expect(204, done)
71+
})
72+
})
73+
74+
describe("/update/channel/alpha/osx", function () {
75+
test("should update from 0.9.1-beta to 1.1.0-alpha.0", function (done) {
76+
agent
77+
.get("/update/channel/alpha/osx/0.9.1-beta")
78+
.expect("Content-Type", /json/)
79+
.expect(function (res) {
80+
expect(res.body.name).toBe("1.1.0-alpha.0")
81+
})
82+
.expect(200, done)
83+
})
84+
})
85+
})
86+
})

0 commit comments

Comments
 (0)