Пример #1
0
    it('verify adding and getting nodes', function(callback) {
        var graph = new AuthzGraph();

        // Verify adding a node returns the node with its id populated and extra attributes
        var a = graph.addNode('a', {'myprop': 'myval'});
        assert.ok(a);
        assert.strictEqual(a.id, 'a');
        assert.strictEqual(a.myprop, 'myval');

        // Verify trying to re-add the node has no impact on the existing node
        assert.ok(!graph.addNode('a', {'myprop': 'anotherval', 'anotherprop': 'anotherval'}));
        a = graph.getNode('a');
        assert.ok(a);
        assert.strictEqual(a.id, 'a');
        assert.strictEqual(a.myprop, 'myval');
        assert.ok(!a.anotherprop);

        // Add a second node
        var b = graph.addNode('b');
        assert.ok(b);
        assert.strictEqual(b.id, 'b');

        // Ensure we have 2 nodes
        var nodes = graph.getNodes();
        assert.strictEqual(nodes.length, 2);
        assert.strictEqual(nodes[0].id, 'a');
        assert.strictEqual(nodes[0].myprop, 'myval');
        assert.strictEqual(nodes[1].id, 'b');

        return callback();
    });
Пример #2
0
    it('verify deleting nodes and edges', function(callback) {
        var graph = new AuthzGraph();

        // Verify a simple graph gets created
        var a = graph.addNode('a');
        var b = graph.addNode('b');
        var edge = graph.addEdge('a', 'b');
        assert.strictEqual(graph.getNodes().length, 2);
        assert.strictEqual(graph.getEdges().length, 1);

        // Verify removing a node removes the node and its associated edges
        graph.removeNode('a');
        assert.strictEqual(graph.getNodes().length, 1);
        assert.strictEqual(graph.getNodes()[0].id, 'b');
        assert.strictEqual(graph.getEdges().length, 0);

        // Verify adding the node back doesn't restore an edge or something weird
        a = graph.addNode('a');
        assert.strictEqual(a.id, 'a');
        assert.strictEqual(graph.getNodes().length, 2);
        assert.strictEqual(graph.getEdges().length, 0);

        // Verify we can add the edge back
        edge = graph.addEdge('a', 'b');
        assert.strictEqual(graph.getEdges().length, 1);
        assert.strictEqual(edge.from.id, 'a');
        assert.strictEqual(edge.to.id, 'b');

        // Verify deleting the edge removes just the edge and the nodes are intact
        graph.removeEdge('a', 'b');
        assert.strictEqual(graph.getNodes().length, 2);
        assert.strictEqual(graph.getEdges().length, 0);

        return callback();
    });
Пример #3
0
    it('verify inbound and outbound traversal', function(callback) {
        var graph = new AuthzGraph();

        // Create a circular graph a-i that is circular, but also links vowels from lowest to
        // highest
        graph.addNode('a');
        graph.addNode('b');
        graph.addNode('c');
        graph.addNode('d');
        graph.addNode('e');
        graph.addNode('f');
        graph.addNode('g');
        graph.addNode('h');
        graph.addNode('i');

        // Create the circle
        graph.addEdge('a', 'b');
        graph.addEdge('b', 'c');
        graph.addEdge('c', 'd');
        graph.addEdge('d', 'e');
        graph.addEdge('e', 'f');
        graph.addEdge('f', 'g');
        graph.addEdge('g', 'h');
        graph.addEdge('h', 'i');
        graph.addEdge('i', 'a');

        // Create the vowel edges
        graph.addEdge('a', 'e');
        graph.addEdge('e', 'i');

        // Verify that the inbound and outbound traversals are depth first and does not repeat
        assert.deepEqual(_.pluck(graph.traverseIn('a'), 'id'), ['a', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b']);
        assert.deepEqual(_.pluck(graph.traverseOut('a'), 'id'), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']);

        // Verify that if we delete the circular graph edges from the vowels, it still finds
        // some different paths around
        graph.removeEdge('d', 'e');
        graph.removeEdge('h', 'i');
        assert.deepEqual(_.pluck(graph.traverseIn('a'), 'id'), ['a', 'i', 'e']);
        assert.deepEqual(_.pluck(graph.traverseOut('a'), 'id'), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']);

        // Delete 'e', ensure paths are broken pretty good
        graph.removeNode('e');
        assert.deepEqual(_.pluck(graph.traverseIn('a'), 'id'), ['a', 'i']);
        assert.deepEqual(_.pluck(graph.traverseOut('a'), 'id'), ['a', 'b', 'c', 'd']);

        // Remove the last of the edges that are connecting to 'a', and ensure we still traverse
        // over 'a' as a single array result
        graph.removeNode('i');
        graph.removeNode('b');
        assert.deepEqual(_.pluck(graph.traverseIn('a'), 'id'), ['a']);
        assert.deepEqual(_.pluck(graph.traverseOut('a'), 'id'), ['a']);

        return callback();
    });
Пример #4
0
    it('verify adding and getting edges', function(callback) {
        var graph = new AuthzGraph();

        // Add 2 nodes to test with
        var a = graph.addNode('a');
        var b = graph.addNode('b');

        // Verify we have 2 nodes and no edges
        var nodes = graph.getNodes();
        var edges = graph.getEdges();
        assert.strictEqual(nodes.length, 2);
        assert.strictEqual(edges.length, 0);

        // Verify adding an edge from a->b returns the edge with it's from/to and extra attributes
        var edge = graph.addEdge('a', 'b', {'myprop': 'myval'});
        assert.ok(edge);
        assert.ok(edge.from);
        assert.strictEqual(edge.from.id, 'a');
        assert.ok(edge.to);
        assert.strictEqual(edge.to.id, 'b');
        assert.strictEqual(edge.myprop, 'myval');

        // Verify trying to re-add the edge has no impact on the existing edge
        assert.ok(!graph.addEdge('a', 'b', {'myprop': 'anotherval', 'anotherprop': 'anoterval'}));
        edge = graph.getEdge('a', 'b');
        assert.ok(edge);
        assert.ok(edge.from);
        assert.strictEqual(edge.from.id, 'a');
        assert.ok(edge.to);
        assert.strictEqual(edge.to.id, 'b');
        assert.strictEqual(edge.myprop, 'myval');
        assert.ok(!edge.anotherprop);

        edges = graph.getEdges();
        assert.strictEqual(edges.length, 1);
        edge = edges[0];
        assert.ok(edge.from);
        assert.strictEqual(edge.from.id, 'a');
        assert.ok(edge.to);
        assert.strictEqual(edge.to.id, 'b');
        assert.strictEqual(edge.myprop, 'myval');
        assert.ok(!edge.anotherprop);

        return callback();
    });
Пример #5
0
    it('verify isEmpty', function(callback) {
        var graph = new AuthzGraph();

        assert.strictEqual(graph.isEmpty(), true);

        graph.addNode('a');
        assert.strictEqual(graph.isEmpty(), false);

        graph.removeNode('a');
        assert.strictEqual(graph.isEmpty(), true);

        return callback();
    });