'use strict';

const JoinCompanyTest = require('./join_company_test');
const BoundAsync = require(process.env.CSSVC_BACKEND_ROOT + '/shared/server_utils/bound_async');
const Assert = require('assert');
const UserTestConstants = require(process.env.CSSVC_BACKEND_ROOT + '/api_server/modules/users/test/user_test_constants');

class JoinCompanyLoginTest extends JoinCompanyTest {

	get description () {
		const by = this.byDomainJoining ? 'domain joining' : 'invite';
		const unifiedIdentity = this.unifiedIdentityEnabled ? ', under unified identity' : '';
		return `should be able to login after joining a company by ${by}${unifiedIdentity}`;
	}

	get method () {
		return 'put';
	}

	setPath (callback) {
		this.path = '/login';
		callback();
	}
	
	getExpectedFields () {
		return { ...UserTestConstants.EXPECTED_LOGIN_RESPONSE };
	}

	// before the test runs...
	before (callback) {
		BoundAsync.series(this, [
			super.before,	// do the usual test prep
			this.doJoin	// perform the actual join
		], callback);
	}

	// validate that the response is correct
	validateResponse (data) {
		Assert.strictEqual(data.user.id, this.joinResponse.userId, 'returned user not equal to the joined user');
		Assert.strictEqual(data.teams.length, 1, '1 and only 1 team should be returned');
		Assert.strictEqual(data.teams[0].id, this.joinResponse.teamId, 'returned team not equal to the expected team');
		if (this.unifiedIdentityEnabled) {
			Assert(typeof data.user.nrUserId === 'number' && data.user.nrUserId, 'nrUserId not set');
			Assert(typeof data.user.nrUserInfo === 'object' && data.user.nrUserInfo.userTier === 'basic_user_tier', 'user tier not set');
			Assert(data.user.nrUserInfo.userTierId === 0, 'user tier not set');
		}
	}
}

module.exports = JoinCompanyLoginTest;
