using System;
using System.Collections;
using System.Data;
using System.Threading;
using System.IO;
using System.Data.SqlClient;
using edu.jhu.ZTestS;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Servers;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;
using NUnit.Core;
using NUnit.Framework;
using System.ComponentModel;
namespace edu.jhu.Cas.WebTest {
    [TestFixture]
    [Description("non-query related system stuff, like groups, account creation, etc")]
    public class CasJobsSysTests {
        //this is failing, but groups seem to work anyway
        //[Test]
        [Description("Creates a new user, then creates a table in mydb as that user, then creates a group, then invites default_wsid to that group, then sees if default_wsid can ref that table, then depublishes table, deletes groups and deletes user.")]
        public void Groups() {
            CasUsers cu = new CasUsers();
            CasGroups cg = new CasGroups();
            CasJobs cj = new CasJobs();
            throw new Exception("disabled until groups updated");
            long wsid = cu.CreateAccount(ID.SERVICE_ID, "fakeuser", "alsdkjfakldj fjsakl", "asdfasd@asdf.com", "abc", "Query,MyDB,Group", 0,1);
            try {
                cj.SubmitShortJob(wsid, "select 123 as fake into faketable", "mydb", "asdf", 0);
                long gid = cg.CreateGroup(wsid, "fakegroup", "laksdjf");
                try {
                    //cg.PublishTable(wsid, "faketable");
                    cg.InviteUser(wsid, CJobsTestLoader.wsid, gid);
                    cg.AcceptInvite(CJobsTestLoader.wsid, gid);
                    DataSet ds = cj.SubmitShortJob(CJobsTestLoader.wsid, "select * from group.fakeuser.faketable", "mydb", "asdf", 0);
                    Assert.AreEqual((int)ds.Tables[0].Rows[0][0], 123);
                    //cg.DePublishTable(wsid, "faketable");
                }
                finally {
                    cg.DestroyGroup(wsid, gid);
                }
            }
            finally {
                cu.DeleteAccount(ID.SERVICE_ID, wsid);
            }
        }
        [Test]
        [Description("Create a user, update profile, drop user.")]
        public void UpdateProfile() {
            CasUsers cu = new CasUsers();
            long wsid = cu.CreateAccount(ID.SERVICE_ID, "fakeuser","name", "e@e.com","pass", "Query,MyDB,Group", 0,1);
            try {
                User u = cu.FindUser(ID.SERVICE_ID, "webservicesid = " + wsid)[0];
                Assert.AreEqual(u.Email, "e@e.com");
                Assert.AreEqual(u.UserID.Trim(), "fakeuser");
                Assert.AreEqual(u.Name, "name");
                Assert.AreEqual(u.Privileges, "Query,MyDB,Group");
                Assert.AreEqual(u.PWEncrypted, Encryptor.MD52Base64Encrypt("pass"));
                u.Email= "email2@e.com";
                cu.UpdateAccount(wsid, u);
                u = cu.FindUser(ID.SERVICE_ID, "webservicesid = " + wsid)[0];
                Assert.AreEqual(u.Email, "email2@e.com");
                Assert.AreEqual(u.UserID.Trim(), "fakeuser");
                Assert.AreEqual(u.Name, "name");
                Assert.AreEqual(u.Privileges, "Query,MyDB,Group");
                Assert.AreEqual(u.PWEncrypted, Encryptor.MD52Base64Encrypt("pass"));
            }
            finally {
                cu.DeleteAccount(ID.SERVICE_ID, wsid);
            }
        }
    }
}