// provider-test.js
//
// Test the provider module
//
// Copyright 2012, StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var assert = require('assert'),
    vows = require('vows'),
    databank = require('databank'),
    schema = require('../lib/schema'),
    URLMaker = require('../lib/urlmaker').URLMaker,
    methodContext = require('./lib/methods').methodContext,
    Databank = databank.Databank,
    DatabankObject = databank.DatabankObject;

vows.describe('provider module interface').addBatch({

    'When we get the provider module': {

        topic: function() { 
            var cb = this.callback;
            // Need this to make IDs

            URLMaker.hostname = "example.net";

            // Dummy databank

            var params = {schema: schema};

            var db = Databank.get('memory', params);

            db.connect({}, function(err) {
                var mod;

                DatabankObject.bank = db;
                
                mod = require('../lib/provider');

                cb(null, mod);
            });
        },
        'there is one': function(err, mod) {
            assert.isObject(mod);
        },
        'and we get its Provider export': {
            topic: function(mod) {
                return mod.Provider;
            },
            'it exists': function(Provider) {
                assert.isFunction(Provider);
            },
            'and we create a new Provider': {
                topic: function(Provider) {
                    return new Provider();
                },
                'it exists': function(provider) {
                    assert.isObject(provider);
                },
                'and we check its methods': methodContext(['previousRequestToken',
                                                           'tokenByConsumer',
                                                           'applicationByConsumerKey',
                                                           'fetchAuthorizationInformation',
                                                           'validToken',
                                                           'tokenByTokenAndVerifier',
                                                           'validateNotReplay',
                                                           'userIdByToken',
                                                           'authenticateUser',
                                                           'associateTokenToUser',
                                                           'generateRequestToken',
                                                           'generateAccessToken',
                                                           'cleanRequestTokens'])
           }
        }
    }
}).export(module);


@@ -827,16 +827,140 @@
 LMaker,%0A
+    Client = require('../lib/method/client').Client,%0A    RequestToken = require('../lib/method/requesttoken').RequestToken,%0A
     meth
@@ -1092,16 +1092,40 @@
 bject;%0A%0A
+var testClient = null;%0A%0A
 vows.des
@@ -1604,16 +1604,17 @@
 k = db;%0A
+%0A
         
@@ -1621,17 +1621,259 @@
         
-%0A
+Client.create(%7Btitle: %22Test App%22, description: %22App for testing%22%7D, function(err, client) %7B%0A                    if (err) %7B%0A                        cb(err, null);%0A                    %7D else %7B%0A                        testClient = client;%0A        
         
@@ -1914,17 +1914,21 @@
 ider');%0A
-%0A
+     
         
@@ -1935,16 +1935,19 @@
         
+   
 cb(null,
@@ -1949,24 +1949,66 @@
 null, mod);%0A
+                    %7D%0A                %7D);%0A
             
@@ -3680,17 +3680,2005 @@
 okens'%5D)
-%0A
+,%0A                'and we use previousRequestToken() on a previously unseen token': %7B%0A                    topic: function(provider) %7B%0A                        provider.previousRequestToken(%22ZZZZZZZZZZZZZZZZZZZZZ%22, this.callback);%0A                    %7D,%0A                    'it returns correct value': function(err, token) %7B%0A                        assert.ifError(err);%0A                        assert.isString(token);%0A                        assert.equal(token, %22ZZZZZZZZZZZZZZZZZZZZZ%22);%0A                    %7D%0A                %7D,%0A                'and we use previousRequestToken() on a previously seen token': %7B%0A                    topic: function(provider) %7B%0A                        var cb = this.callback,%0A                            props = %7Bconsumer_key: testClient.consumer_key,%0A                                     callback: 'http://example.com/callback/12345/'%7D;%0A%0A                        RequestToken.create(props, function(err, rt) %7B%0A                            if (err) %7B%0A                                cb(err, null);%0A                            %7D else %7B%0A                                provider.previousRequestToken(rt.token, function(err, token) %7B%0A                                    if (err) %7B // this is correct!%0A                                        cb(null, rt);%0A                                    %7D else %7B%0A                                        cb(new Error(%22Didn't throw an error%22), null);%0A                                    %7D%0A                                %7D);%0A                            %7D%0A                        %7D);%0A                    %7D,%0A                    'it fails correctly': function(err, rt) %7B%0A                        assert.ifError(err);%0A                        assert.isObject(rt);%0A                    %7D,%0A                    teardown: function(requestToken) %7B%0A                        if (requestToken && requestToken.del) %7B%0A                            requestToken.del(function(err) %7B%7D);%0A                        %7D%0A                    %7D%0A                %7D%0A 
         

Test previousRequestToken()