/
whoami.test.js
122 lines (101 loc) Β· 3.89 KB
/
whoami.test.js
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
const {
fs: {createTemporaryFolder, readFile, writeFile},
tests: {startPackageServer}
} = require('pkg-tests-core');
const AUTH_TOKEN = `686159dc-64b3-413e-a244-2de2b8d1c36f`;
const AUTH_IDENT = `dXNlcm5hbWU6YSB2ZXJ5IHNlY3VyZSBwYXNzd29yZA==`;
const INVALID_AUTH_TOKEN = `a24cb960-e6a5-45fc-b9ab-0f9fe0aaae57`;
const INVALID_AUTH_IDENT = `dXNlcm5hbWU6bm90IHRoZSByaWdodCBwYXNzd29yZA==`; // username:not the right password
describe(`Commands`, () => {
describe(`npm whoami`, () => {
test(
`it should print the npm registry username when config has a valid npmAuthToken`,
makeTemporaryEnv({}, async ({ path, run, source }) => {
await writeFile(`${path}/.yarnrc.yml`, `npmAuthToken: "${AUTH_TOKEN}"\n`);
let code;
let stdout;
let stderr;
try {
({code, stdout, stderr} = await run(`npm`, `whoami`));
} catch (error) {
({code, stdout, stderr} = error);
}
expect({code, stdout, stderr}).toMatchSnapshot();
})
);
test(
`it should print the npm registry username when config has a valid npmAuthIdent`,
makeTemporaryEnv({}, async ({ path, run, source }) => {
await writeFile(`${path}/.yarnrc.yml`, `npmAuthIdent: "${AUTH_IDENT}"\n`);
let code;
let stdout;
let stderr;
try {
({code, stdout, stderr} = await run(`npm`, `whoami`));
} catch (error) {
({code, stdout, stderr} = error);
}
expect({code, stdout, stderr}).toMatchSnapshot();
})
);
test(
`it should print the npm registry username for a given scope`,
makeTemporaryEnv({}, async ({ path, run, source }) => {
const url = startPackageServer();
await writeFile(`${path}/.yarnrc.yml`, [
`npmScopes:\n`,
` testScope:\n`,
` npmRegistryServer: "${url}"\n`,
`npmRegistries:\n`,
` "${url}":\n`,
` npmAuthToken: ${AUTH_TOKEN}`,
].join(``));
let code;
let stdout;
let stderr;
try {
({code, stdout, stderr} = await run(`npm`, `whoami`, `--scope`, `testScope`));
} catch (error) {
({code, stdout, stderr} = error);
}
expect({code, stdout, stderr}).toMatchSnapshot();
})
);
test(
`it should throw an error when no auth config is found`,
makeTemporaryEnv({}, async ({ path, run, source }) => {
await expect(run(`npm`, `whoami`)).rejects.toThrowError(/No authentication configured/);
})
);
test(
`it should throw an error when config has an invalid npmAuthToken`,
makeTemporaryEnv({}, async ({ path, run, source }) => {
await writeFile(`${path}/.yarnrc.yml`, `npmAuthToken: "${INVALID_AUTH_TOKEN}"\n`);
await expect(run(`npm`, `whoami`)).rejects.toThrowError(/Authentication failed/);
})
);
test(
`it should throw an error when config has an invalid npmAuthIdent`,
makeTemporaryEnv({}, async ({ path, run, source }) => {
await writeFile(`${path}/.yarnrc.yml`, `npmAuthIdent: "${INVALID_AUTH_IDENT}"\n`);
await expect(run(`npm`, `whoami`)).rejects.toThrowError(/Authentication failed/);
})
);
test(
`it should thow an error when invalid auth config is found for a scope`,
makeTemporaryEnv({}, async ({ path, run, source }) => {
const url = startPackageServer();
await writeFile(`${path}/.yarnrc.yml`, [
`npmScopes:\n`,
` testScope:\n`,
` npmRegistryServer: "${url}"\n`,
`npmRegistries:\n`,
` "${url}":\n`,
` npmAuthToken: ${INVALID_AUTH_TOKEN}`,
].join(``));
await expect(run(`npm`, `whoami`, `--scope`, `testScope`)).rejects.toThrowError(/Authentication failed/);
await expect(run(`npm`, `whoami`)).rejects.toThrowError(/Authentication failed/);
})
);
});
});