using System;
using System.Threading;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;
using System.Data;
using edu.jhu.Cas.Servers;
using NUnit.Framework;
using NUnit.Core;
using System.Configuration;
using System.Data.SqlClient;
/**
 * output tests
 * these test for succesful extraction but do not test validity or if you can actually dl the file
*/
namespace edu.jhu.Cas.Test{
	[TestFixture]	
	public class AccountTests{
		
		private CasJobs cJobs;
		private CasUsers cUsers;
		private const long wsid = 193424723;
		private string userid = "ASDGFDSJSKLSDFKJ";

		[TestFixtureSetUp]
		public void Init(){
			cJobs = new CasJobs();
			cUsers = new CasUsers();
		}
		
		[Test]
		public void CreateModifyDeleteAccount(){
			string name = "asdfdas",email="asdlffjklaaskdjf",pw="cvxzxcv",privs="Query,MyDB,Group";
			long newWsid = cUsers.CreateAccount(ID.SERVICE_ID,userid,name,email,pw,privs,0);
			try{
				User u = cUsers.FindUser(ID.SERVICE_ID,"webservicesid = "+newWsid)[0];
				Assert.AreEqual(name,u.Name);
				Assert.AreEqual(email,u.Email);
				Assert.AreEqual(privs,u.Privileges);
				string n1="vad",e2="gbf",pw2="dfjskl";
				u.Name = n1;u.Email = e2;u.PWEncrypted=pw2;
				cUsers.UpdateAccount(newWsid,u,true);
				u = cUsers.FindUser(ID.SERVICE_ID,"webservicesid = "+newWsid)[0];
				Assert.IsTrue(u.Email.CompareTo(e2) == 0);
				Assert.IsTrue(u.Name.CompareTo(n1) == 0);
				//Assert.IsTrue(u.Password.CompareTo(pw2) == 0);
			}finally{
				cUsers.DeleteAccount(ID.SERVICE_ID,newWsid);
			}
		}

		[Test] 
		public void DESPasswordEncryption(){
			Encryptor encryptor = new Encryptor();
			string password = "yoyoyasdfasdfo";
			byte[] encrypted= encryptor.TripleDESEncrypt(password);
			//Console.WriteLine(encrypted);
			string decrypted= encryptor.TripleDESDecrypt(encrypted);
			Console.WriteLine(decrypted);
			Assert.AreEqual(decrypted,password);
		}
		
		[Test] 
		public void MD5PasswordEncryption(){
			Encryptor encryptor = new Encryptor();
			string password = "yoyoyasdfasdfo";
			string encrypted= encryptor.MD52Base64Encrypt(password);
		}

		//[Test]
		public void encryptpasswords(){
			DataSet ds = (new CasUsers()).FindUserDataSet(ID.SERVICE_ID,"");
			Encryptor encryptor = new Encryptor();
			foreach(DataRow dr in ds.Tables[0].Rows){
				string password = dr["password"].ToString();
				if(password.Length > 0){
					Console.Write(password);
					string encrypted = encryptor.MD52Base64Encrypt(password);
					SqlCommand com = new SqlCommand();
					com.Parameters.Add("@encpw",SqlDbType.VarChar,50);
					com.Parameters["@encpw"].Value = encrypted;
					com.CommandText = "update users set pwencrypted = @encpw where webservicesid = "+dr["webservicesid"];
					com.Connection = new SqlConnection(SqlFactory.JOBS_STRING);
					com.Connection.Open();
					com.ExecuteNonQuery();
					com.Connection.Close();
				}
			}

		}

	}
}