Skip to content

Instantly share code, notes, and snippets.

@leefsmp
Created October 2, 2015 22:54
Show Gist options
  • Save leefsmp/36617f7e0a34e91f95be to your computer and use it in GitHub Desktop.
Save leefsmp/36617f7e0a34e91f95be to your computer and use it in GitHub Desktop.
A mocha/chai test for view-and-data npm package
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Philippe Leefsma 2015 - ADN/Developer Technical Services
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
var should = require('chai').should(),
config = require('../config-view-and-data'),
Lmv = require('../view-and-data'),
path = require('path'),
fs = require('fs');
describe('# View & Data Tests: ', function() {
///////////////////////////////////////////////////////////////////
// Test authentication token
//
///////////////////////////////////////////////////////////////////
it('Get token', function(done) {
//set a 15'' timeout
this.timeout(15 * 1000);
var lmv = new Lmv(config);
lmv.getToken().then(function(response) {
done();
}, function(error) {
done(error);
});
});
///////////////////////////////////////////////////////////////////
// Test bucket access or creation
//
///////////////////////////////////////////////////////////////////
it('Get bucket (create if does not exist)', function(done) {
this.timeout(15 * 1000);
var lmv = new Lmv(config);
function onError(error) {
done(error);
}
function onInitialized(response) {
var createIfNotExists = true;
var bucketCreationData = {
bucketKey: config.defaultBucketKey,
servicesAllowed: {},
policy: "transient"
};
lmv.getBucket(config.defaultBucketKey,
createIfNotExists,
bucketCreationData).then(
onBucketCreated,
onError);
}
function onBucketCreated(response) {
done();
}
lmv.initialise().then(onInitialized, onError);
});
///////////////////////////////////////////////////////////////////
// Test Full workflow
//
///////////////////////////////////////////////////////////////////
it('Full workflow (bucket/upload/registration/translation/thumbnail)', function(done) {
this.timeout(5 * 60 * 1000); //5 mins timeout
var urn = '';
var lmv = new Lmv(config);
function onError(error) {
done(error);
}
function onInitialized(response) {
var createIfNotExists = true;
var bucketCreationData = {
bucketKey: config.defaultBucketKey,
servicesAllowed: {},
policy: "transient"
};
lmv.getBucket(config.defaultBucketKey,
createIfNotExists,
bucketCreationData).then(
onBucketCreated,
onError);
}
function onBucketCreated(response) {
lmv.upload(
path.join(__dirname, './data/test.dwf'),
config.defaultBucketKey,
'test.dwf').then(onUploadCompleted, onError);
}
function onUploadCompleted(response) {
var fileId = response.objects[0].id;
urn = lmv.toBase64(fileId);
lmv.register(urn, true).then(onRegister, onError);
}
function onRegister(response) {
if (response.Result === "Success") {
console.log('Translating file...');
lmv.checkTranslationStatus(
urn, 1000 * 60 * 5, 1000 * 10,
progressCallback).then(
onTranslationCompleted,
onError);
}
else {
done(response);
}
}
function progressCallback(progress) {
console.log(progress);
}
function onTranslationCompleted(response) {
console.log('URN: ' + response.urn);
lmv.getThumbnail(urn).then(onThumbnail, onError);
}
function onThumbnail(response) {
done();
}
//start the test
lmv.initialise().then(onInitialized, onError);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment