// oauth-test.js
//
// Test the client registration API
//
// 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'),
    Step = require('step'),
    _ = require('underscore'),
    querystring = require('querystring'),
    http = require('http'),
    OAuth = require('oauth').OAuth,
    httputil = require('./lib/http'),
    oauthutil = require('./lib/oauth'),
    requestToken = oauthutil.requestToken,
    setupApp = oauthutil.setupApp,
    newClient = oauthutil.newClient;

var suite = vows.describe('user API');

suite.addBatch({
    'When we set up the app': {
        topic: function() {
            setupApp(this.callback);
        },
        teardown: function(app) {
            if (app && app.close) {
                app.close();
            }
        },
        'it works': function(err, app) {
            assert.ifError(err);
        },
        'and we request a token with no Authorization': {
            topic: function() {
                var cb = this.callback;
                httputil.post('localhost', 4815, '/oauth/request_token', {}, function(err, res, body) {
                    if (err) {
                        cb(err);
                    } else if (res.statusCode === 400) {
                        cb(null);
                    } else {
                        cb(new Error("Unexpected success"));
                    }
                });
            },
            'it fails correctly': function(err, cred) {
                assert.ifError(err);
            }
        },
        'and we request a token with an invalid client_id': {
            topic: function() {
                var cb = this.callback,
                    badcl = {client_id: "NOTACLIENTID",
                             client_secret: "NOTTHERIGHTSECRET"};

                requestToken(badcl, function(err, rt) {
                    if (err) {
                        cb(null);
                    } else {
                        cb(new Error("Unexpected success"));
                    }
                });
            },
            'it fails correctly': function(err, cred) {
                assert.ifError(err);
            }
        },
        'and we create a client using the api': {
            topic: function() {
                newClient(this.callback);
            },
            'it works': function(err, cl) {
                assert.ifError(err);
                assert.isObject(cl);
                assert.isString(cl.client_id);
                assert.isString(cl.client_secret);
            },
            'and we request a token with a valid client_id and invalid client_secret': {
                topic: function(cl) {
                    var cb = this.callback,
                        badcl = {client_id: cl.client_id,
                                 client_secret: "NOTTHERIGHTSECRET"};

                    requestToken(badcl, function(err, rt) {
                        if (err) {
                            cb(null);
                        } else {
                            cb(new Error("Unexpected success"));
                        }
                    });
                },
                'it fails correctly': function(err, cred) {
                    assert.ifError(err);
                }
            },
            'and we request a request token with valid client_id and client_secret': {
                topic: function(cl) {
                    requestToken(cl, this.callback);
                },
                'it works': function(err, cred) {
                    assert.ifError(err);
                    assert.isObject(cred);
                },
                'it has the right results': function(err, cred) {
                    assert.include(cred, 'token');
                    assert.isString(cred.token);
                    assert.include(cred, 'token_secret');
                    assert.isString(cred.token_secret);
                }
            }
        }
    }
});

suite['export'](module);
@@ -2,16 +2,30 @@
 / oauth-
+request-token-
 test.js%0A
@@ -43,31 +43,37 @@
 the 
-client registration API
+OAuth request token interface
 %0A//%0A
@@ -4538,24 +4538,1372 @@
 %0A    %7D%0A%7D);%0A%0A
+// A batch to test parallel requests%0A%0Asuite.addBatch(%7B%0A    'When we set up the app': %7B%0A        topic: function() %7B%0A            setupApp(this.callback);%0A        %7D,%0A        teardown: function(app) %7B%0A            if (app && app.close) %7B%0A                app.close();%0A            %7D%0A        %7D,%0A        'it works': function(err, app) %7B%0A            assert.ifError(err);%0A        %7D,%0A        'and we create a client using the api': %7B%0A            topic: function() %7B%0A                newClient(this.callback);%0A            %7D,%0A            'it works': function(err, cl) %7B%0A                assert.ifError(err);%0A            %7D,%0A            'and we get two request tokens in series': %7B%0A                topic: function(cl) %7B%0A                    var cb = this.callback;%0A                    Step(%0A                        function() %7B%0A                            requestToken(cl, this);%0A                        %7D,%0A                        function(err, rt1) %7B%0A                            if (err) throw err;%0A                            requestToken(cl, this);%0A                        %7D,%0A                        function(err, rt2) %7B%0A                            cb(err);%0A                        %7D%0A                    );%0A                %7D,%0A                'it works': function(err) %7B%0A                    assert.ifError(err);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%7D);%0A%0A
 suite%5B'expor

Test getting tokens in series