Skip to content

Commit a4c8ef8

Browse files
committed
Added folder (api doesn't care) and test refactoring
1 parent 1da0167 commit a4c8ef8

File tree

3 files changed

+78
-44
lines changed

3 files changed

+78
-44
lines changed

bin/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,36 +78,44 @@ yargs(hideBin(process.argv))
7878
.option('f', {
7979
alias: 'file',
8080
describe: 'the path to the file (relative) [REQUIRED OR -s]',
81+
requiresArg: true,
8182
})
8283
.option('s', {
8384
alias: 'string',
8485
describe: 'the string you want to paste [REQUIRED OR -f]',
86+
requiresArg: true,
8587
})
8688
.option('n', {
8789
alias: 'name',
8890
describe: 'the name of your paste',
8991
default: 'Untitled',
92+
requiresArg: true,
9093
})
9194
.option('form', {
9295
alias: 'format',
9396
describe: 'syntax highlighting of your paste',
9497
default: 'text',
98+
requiresArg: true,
9599
})
96100
.option('v', {
97101
alias: 'visibility',
98102
describe: 'whether your paste should be public, private or unlisted',
99103
default: 'unlisted',
100104
choices: ['unlisted', 'private', 'public'],
105+
requiresArg: true,
101106
})
102107
.option('e', {
103108
alias: 'expiry',
104109
describe: 'when your paste should expire',
105110
default: 'N',
106111
choices: ['N', '10M', '1H', '1D', '1W', '2W', '1M', '6M', '1Y'],
112+
requiresArg: true,
107113
})
108114
.option('fol', {
109115
alias: 'folder',
110116
describe: 'the folder of your paste',
117+
default: '',
118+
requiresArg: true,
111119
})
112120
.conflicts('f', 's');
113121
},

lib/api-functions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,15 @@ async function createPaste(argv, apiToken, userToken) {
126126
return 'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats';
127127
}
128128

129+
if (folder.length > 8) {
130+
return 'Error! Pastebin only allows up to 8 characters for a folder name.';
131+
}
132+
129133
const mappedVisibility = mapToVisiblityCode(visibility);
130134
const pasteText = file ? readFileSync(file, 'utf-8').trim() : string;
131135

132136
const response = await fetch(API_URLS.apiPost, {
133-
body: `api_dev_key=${apiToken}&api_user_key=${userToken}&api_option=paste&api_paste_code=${pasteText}&api_paste_name=${name}&api_paste_format=${format}&api_paste_private=${mappedVisibility}`,
137+
body: `api_dev_key=${apiToken}&api_user_key=${userToken}&api_option=paste&api_paste_code=${pasteText}&api_paste_name=${name}&api_paste_format=${format}&api_paste_private=${mappedVisibility}&api_paste_expire_date=${expiry}&api_folder_key=${folder}`,
134138
headers: {
135139
'Content-Type': 'application/x-www-form-urlencoded',
136140
},

tests/api-functions.spec.js

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ describe('apiFunctions', () => {
4343
);
4444

4545
it('should return table of pastes', async () => {
46-
const apiRes = await listPastes(1, 'paste key', 'paste name');
46+
const actualResponse = await listPastes(1, 'paste key', 'paste name');
4747

4848
// this sucks, but hardcoding the expected table as a string
4949
// is even worse because of newlines, etc.
50-
expect(apiRes).toBe(
50+
expect(actualResponse).toBe(
5151
table([
5252
[...TABLE_HEADERS],
5353
['paste key', 'paste name', 'public', 'Never', 'text'], // values set in node-fetch.js mock
@@ -59,9 +59,9 @@ describe('apiFunctions', () => {
5959
__setStatusCode(500);
6060
__setResponseText('API Error');
6161

62-
const apiRes = await listPastes(1, 'testi', 'testo');
62+
const actualResponse = await listPastes(1, 'testi', 'testo');
6363

64-
expect(apiRes).toBe('Error! API Error');
64+
expect(actualResponse).toBe('Error! API Error');
6565
});
6666
});
6767

@@ -81,13 +81,17 @@ describe('apiFunctions', () => {
8181
);
8282

8383
it('should return successfull response if paste was deleted', async () => {
84-
const responseText = 'Paste removed';
84+
const expectedResponse = 'Paste removed';
8585

86-
__setResponseText(responseText);
86+
__setResponseText(expectedResponse);
8787

88-
const apiRes = await deletePaste('paste key', 'api token', 'user token');
88+
const actualResponse = await deletePaste(
89+
'paste key',
90+
'api token',
91+
'user token'
92+
);
8993

90-
expect(apiRes).toBe(`Success! ${responseText}`);
94+
expect(actualResponse).toBe(`Success! ${expectedResponse}`);
9195
});
9296

9397
it('should return error if api returns error', async () => {
@@ -96,9 +100,13 @@ describe('apiFunctions', () => {
96100
__setResponseText(resonseText);
97101
__setStatusCode(403);
98102

99-
const apiRes = await deletePaste('pastey key', 'api token', 'user token');
103+
const actualResponse = await deletePaste(
104+
'pastey key',
105+
'api token',
106+
'user token'
107+
);
100108

101-
expect(apiRes).toBe(`Error! ${resonseText}`);
109+
expect(actualResponse).toBe(`Error! ${resonseText}`);
102110
});
103111
});
104112

@@ -118,28 +126,29 @@ describe('apiFunctions', () => {
118126
);
119127

120128
it('should create paste and return paste url', async () => {
121-
const responseText = 'https://pastebin.com/12345678';
129+
const expectedResponse = 'https://pastebin.com/12345678';
122130

123-
__setResponseText(responseText);
131+
__setResponseText(expectedResponse);
124132

125-
const apiRes = await createPaste(
126-
{ format: 'text', string: 'string' },
133+
const actualResponse = await createPaste(
134+
{ format: 'text', string: 'string', folder: '' },
127135
'api token',
128136
'user token'
129137
);
130138

131-
expect(apiRes).toBe(`Success! ${responseText}`);
139+
expect(actualResponse).toBe(`Success! ${expectedResponse}`);
132140
});
133141

134142
it('should create paste from file', async () => {
135-
const responseText = 'https://pastebin.com/12345678';
143+
const expectedResponse = 'https://pastebin.com/12345678';
136144

137-
__setResponseText(responseText);
145+
__setResponseText(expectedResponse);
138146

139-
const apiRes = await createPaste(
147+
const actualResponse = await createPaste(
140148
{
141149
format: 'text',
142150
file: '/some/path/to/file',
151+
folder: '',
143152
},
144153
'api token',
145154
'user token'
@@ -149,69 +158,82 @@ describe('apiFunctions', () => {
149158
'/some/path/to/file',
150159
'utf-8'
151160
);
152-
expect(apiRes).toBe(`Success! ${responseText}`);
161+
expect(actualResponse).toBe(`Success! ${expectedResponse}`);
153162
});
154163

155164
it('should return error if api returns error', async () => {
156-
const responseText = 'Ran out of storage';
165+
const expectedResponse = 'Ran out of storage';
157166

158-
__setResponseText(responseText);
167+
__setResponseText(expectedResponse);
159168
__setStatusCode(500);
160169

161-
const apiRes = await createPaste(
162-
{ format: 'text', string: 'string' },
170+
const actualResponse = await createPaste(
171+
{ format: 'text', string: 'string', folder: '' },
163172
'api token',
164173
'user token'
165174
);
166175

167-
expect(apiRes).toBe(`Error! ${responseText}`);
176+
expect(actualResponse).toBe(`Error! ${expectedResponse}`);
168177
});
169178

170179
it('should return error if format is not supported by pastebin', async () => {
171180
const resonseText =
172181
'Error! Format option is not supported by pastebin. See https://pastebin.com/doc_api#8 for supported formats';
173182

174-
const apiRes = await createPaste(
175-
{ format: 'does not exist', string: 'string' },
183+
const actualResponse = await createPaste(
184+
{ format: 'does not exist', string: 'string', folder: '' },
185+
'api token',
186+
'user token'
187+
);
188+
189+
expect(actualResponse).toBe(resonseText);
190+
});
191+
192+
it('should return error if folder name is longer than 8 characters', async () => {
193+
const expectedResponse =
194+
'Pastebin only allows up to 8 characters for a folder name.';
195+
196+
const actualResponse = await createPaste(
197+
{ format: 'text', string: 'string', folder: 'longerthan8characters' },
176198
'api token',
177199
'user token'
178200
);
179201

180-
expect(apiRes).toBe(resonseText);
202+
expect(actualResponse).toBe(`Error! ${expectedResponse}`);
181203
});
182204

183205
it('should return error if neither file or string are supplied', async () => {
184-
const responseText =
206+
const expectedResponse =
185207
'You need to supply either -f (--file) OR -s (--string)';
186208

187-
const apiRes = await createPaste(
209+
const actualResponse = await createPaste(
188210
{ format: 'text' },
189211
'api token',
190212
'user token'
191213
);
192214

193-
expect(apiRes).toBe(responseText);
215+
expect(actualResponse).toBe(expectedResponse);
194216
});
195217
});
196218

197219
describe('logOut', () => {
198220
it('should delete the ~/.pasty.user file', () => {
199-
const responseText = 'Successfully logged you out.';
221+
const expectedResponse = 'Successfully logged you out.';
200222

201-
const apiRes = logout();
223+
const actualResponse = logout();
202224

203-
expect(apiRes).toBe(responseText);
225+
expect(actualResponse).toBe(expectedResponse);
204226
expect(fs.rmSync).toHaveBeenCalledWith('mockHomeDir/.pasty.user');
205227
});
206228

207229
it('should return error if there is no ~/.pasty.user file', () => {
208-
const responseText =
230+
const expectedResponse =
209231
"You're currently not logged in (could not find ~/.pasty.user)";
210232

211233
fs.__setFileExists(false);
212-
const apiRes = logout();
234+
const actualResponse = logout();
213235

214-
expect(apiRes).toBe(responseText);
236+
expect(actualResponse).toBe(expectedResponse);
215237
expect(fs.rmSync).not.toHaveBeenCalled();
216238
});
217239
});
@@ -227,29 +249,29 @@ describe('apiFunctions', () => {
227249
it('should write user token to file if login was successfull', async () => {
228250
const username = 'dummyUser';
229251
const tokenFromApi = 'token from api';
230-
const responseText = `You're now logged in as ${username}`;
252+
const expectedResponse = `You're now logged in as ${username}`;
231253

232254
__setResponseText(tokenFromApi);
233255

234-
const apiRes = await loginUser({ username }, 'api token');
256+
const actualResponse = await loginUser({ username }, 'api token');
235257

236258
expect(fs.writeFileSync).toHaveBeenCalledWith(
237259
'mockHomeDir/.pasty.user',
238260
tokenFromApi,
239261
'utf-8'
240262
);
241-
expect(apiRes).toBe(`Success! ${responseText}`);
263+
expect(actualResponse).toBe(`Success! ${expectedResponse}`);
242264
});
243265

244266
it('should return error if api returns error', async () => {
245-
const responseText = 'Login service unavailable';
267+
const expectedResponse = 'Login service unavailable';
246268

247-
__setResponseText(responseText);
269+
__setResponseText(expectedResponse);
248270
__setStatusCode(500);
249271

250-
const apiRes = await loginUser({}, 'api token');
272+
const actualResponse = await loginUser({}, 'api token');
251273

252-
expect(apiRes).toBe(`Error! ${responseText}`);
274+
expect(actualResponse).toBe(`Error! ${expectedResponse}`);
253275
});
254276
});
255277
});

0 commit comments

Comments
 (0)