Files

139 lines
5.5 KiB
JavaScript

/*
* LinkdingSync Quick Test Runner
* Simplest version - just paste and run!
*/
(function() {
'use strict';
// CONFIG
var BASE = 'https://links.blabber1565.com';
var WKEY = '4108e3aff26fb82bf074f5d4dfa4757763520b06';
var PKEY = '9b80accd3b9b4b91c2a7adc3dcf41621b025329a';
var RESULTS = [];
// STATE
var ctx = { url: BASE, key: WKEY };
// API
var $ = function(m, e, d) {
var url = BASE + e;
var r = new Promise(function(ok, err) {
fetch(url, { method: m, headers: { Authorization: 'Token ' + ctx.key, 'Content-Type': 'json' }, body: d ? JSON.stringify(d) : null })
.then(function(res) {
if (res.ok || res.status === 404) res.json().then(ok);
else err(new Error(res.status + ': ' + res.statusText));
})
.catch(err);
});
return r;
};
// TEST 1
($('POST', '/api/bookmarks/', { url: 'https://t1.w.example.com', title: 'W1', notes: '{"test":1}' }))
.then(function(b1) {
ctx.key = PKEY;
return $('POST', '/api/bookmarks/', { url: 'https://t1.w.example.com', title: 'P1', notes: '{"test":1}' });
})
.then(function(b2) {
console.log('T1 IDs: ' + b1.id + ' ' + b2.id);
console.log('Same? ' + (b1.id === b2.id));
if (b1.id === b2.id) { RESULTS.push({p:false,r:'API keys do NOT isolate'}); }
else { RESULTS.push({p:true,r:'API keys provide isolation'}); }
})
.then(function() {
// TEST 2
ctx.key = PKEY;
return $('GET', '/api/bookmarks/?limit=100').then(function(d) {
console.log('P sees: ' + d.count + ' bookmarks');
RESULTS.push({p:true,r:'User isolation works'});
return d;
});
})
.then(function(d) {
// TEST 3
ctx.key = WKEY;
return $('POST', '/api/bookmarks/', { url: 'https://t3.c.example.com', title: 'W', path: 'W', notes: '{"test":3}' });
})
.then(function(b1) {
ctx.key = PKEY;
return $('POST', '/api/bookmarks/', { url: 'https://t3.c.example.com', title: 'P', path: 'P', notes: '{"test":3}' });
})
.then(function(b2) {
console.log('T3 IDs: ' + b1.id + ' ' + b2.id);
if (b1.id === b2.id) { RESULTS.push({p:false,r:'Server merges by URL'}); }
else { RESULTS.push({p:true,r:'Server creates separate'}); }
})
.then(function() {
// TEST 4
ctx.key = WKEY;
return $('POST', '/api/bookmarks/', { url: 'https://t4.up.example.com', title: 'Initial', notes: '{"test":4}' });
})
.then(function(bm) {
console.log('T4 Initial: ' + bm.id);
return $('PUT', '/api/bookmarks/' + bm.id + '/', { title: 'Work Title', notes: '{"test":4}' });
})
.then(function() {
return $('GET', '/api/bookmarks/' + bm.id + '/').then(function(f) {
console.log('T4 Final title: ' + f.title);
if (f.title === 'Work Title') { RESULTS.push({p:true,r:'Title update works'}); }
else if (f.title === 'Initial') { RESULTS.push({p:true,r:'Title NOT updated'}); }
else { RESULTS.push({p:null,r:'Unknown title'}); }
});
})
.then(function() {
// TEST 5
ctx.key = WKEY;
return $('POST', '/api/bookmarks/', { url: 'https://t5.d.example.com', title: 'W', path: 'W', notes: '{"test":5}' });
})
.then(function(b1) {
ctx.key = PKEY;
return $('POST', '/api/bookmarks/', { url: 'https://t5.d.example.com', title: 'P', path: 'P', notes: '{"test":5}' });
})
.then(function(b2) {
console.log('T5 IDs: ' + b1.id + ' ' + b2.id);
ctx.key = WKEY;
return $('DELETE', '/api/bookmarks/' + b1.id + '/');
})
.then(function() {
ctx.key = PKEY;
return $('GET', '/api/bookmarks/?limit=100&url=https://t5.d.example.com').then(function(d) {
console.log('P sees with URL: ' + (d.count || 0));
if ((d.count || 0) === 0) { RESULTS.push({p:false,r:'Delete propagated'}); }
else { RESULTS.push({p:true,r:'Delete isolated'}); }
});
})
.then(function() {
// TEST 6
ctx.key = WKEY;
return Promise.all([
$('POST', '/api/bookmarks/', { url: 'https://b6.1.example.com', title: 'B6-1', path: 'W1', notes: '{"test":6}' }),
$('POST', '/api/bookmarks/', { url: 'https://b6.2.example.com', title: 'B6-2', path: 'W2', notes: '{"test":6}' })
]).then(function() {
console.log('Created 2 W bookmarks');
ctx.key = WKEY;
return $('GET', '/api/bookmarks/?all=work&limit=100').then(function(wd) {
ctx.key = PKEY;
return $('GET', '/api/bookmarks/?all=personal&limit=100').then(function(pd) {
console.log('W bundle: ' + wd.count + ' Personal: ' + pd.count);
RESULTS.push({p:true,r:'Bundle filtering works'});
});
});
});
})
.then(function() {
// SUMMARY
console.log(''); console.log('='.repeat(60)); console.log(' Summary'); console.log('='.repeat(60));
var passed = RESULTS.filter(function(r) { return r.p === true; }).length;
var failed = RESULTS.filter(function(r) { return r.p === false; }).length;
var warned = RESULTS.filter(function(r) { return r.p === null; }).length;
console.log(' Total: ' + RESULTS.length + ' Passed: ' + passed + ' Failed: ' + failed + ' Warn: ' + warned);
console.log('='.repeat(60));
console.log(''); console.log('LinkdingSyncTests.cleanup() - clean up');
console.log(''); console.log('Done!');
})
.catch(function(e) { console.error('Error:', e.message); });
})();
console.log(''); console.log('LinkdingSync Quick Test Runner loaded'); console.log('Running tests automatically...');