/* global AJS:false */
AJS.test.require([
    'com.atlassian.bitbucket.server.bitbucket-web:bitbucket-user-data-provider',
    'com.atlassian.bitbucket.server.bitbucket-it-javascript:js-test-utils'
], function() {
    'use strict';

    var testContext = require('test-context');

    var UserDataProvider = require('bitbucket/internal/impl/data-provider/user');

    QUnit.module('user-data-provider', {
        setup: function() {
            this._context = testContext.context();
            this._context.loginAsAdmin();
            this._context.user({
                name: "user-data-provider-test",
                password: "user-data-provider-test",
                displayName: "user-data-provider-test",
                emailAddress: "user-data-provider-test@example.com"
            });
        },
        teardown: function() {
            this._context.destroy();
        }
    });

    QUnit.test('DataProvider grabs real data', function(assert) {
        var udp = new UserDataProvider({
            filter: {
                permissions: [{
                    name: 'KNOWN_USER'
                }],
                term: 'user-data-provider-test'
            }
        });

        return udp.fetchNext().then(function(users) {
            assert.strictEqual(users.length, 1, 'Returns filtered user list');
            assert.strictEqual(users[0].name, 'user-data-provider-test');
            assert.strictEqual(udp.reachedEnd, true, 'Only one page');
        });
    });

    QUnit.test('DataProvider filters by term', function(assert) {
        var udp = new UserDataProvider({
            filter: {
                permissions: [{
                    name: 'KNOWN_USER'
                }],
                term: 'nothing-matches'
            }
        });

        return udp.fetchNext().then(function(users) {
            assert.strictEqual(users.length, 0, 'Returns filtered user list');
            assert.strictEqual(udp.reachedEnd, true, 'Only one page');
        });
    });

    QUnit.test('DataProvider filters by permission', function(assert) {
        var udp = new UserDataProvider({
            filter: {
                permissions: [{
                    name: 'REPO_ADMIN',
                    repository_id: "1"
                }],
                term: 'user-data-provider-test'
            }
        });

        return udp.fetchNext().then(function(users) {
            assert.strictEqual(users.length, 0, 'Returns filtered user list');
            assert.strictEqual(udp.reachedEnd, true, 'Only one page');
        });
    });

    QUnit.test('DataProvider respects avatarSize', function(assert) {
        var udp = new UserDataProvider({
            avatarSize: 'large',
            filter: {
                permissions: [{
                    name: 'KNOWN_USER'
                }],
                term: 'user-data-provider-test'
            }
        });

        return udp.fetchNext().then(function(users) {
            assert.strictEqual(users.length, 1, 'Returns filtered user list');
            assert.ok(users[0].avatar_url.indexOf('96') !== -1, 'avatar url contains correct pixel value - ' + users[0].avatarUrl);
        });
    });
});
