예제 #1
0
        internal AjaxContext(JsonData input)
        {
            // get the api out...
            string apiKey = input.GetValueSafe<string>("apiKey");
            if (string.IsNullOrEmpty(apiKey))
                throw new InvalidOperationException("The 'apiKey' value was not specified in the request.");
            this.ApiUser = ApiUser.GetOrCreateApiUser(new Guid(apiKey));
            if(ApiUser == null)
                throw new InvalidOperationException("'ApiUser' is null.");

            // do we have a logon token?
            string asString = input.GetValueSafe<string>("logonToken");
            if (!(string.IsNullOrEmpty(asString)))
            {
                Token token = Token.GetByToken(this.ApiUser, asString);
                if (token == null)
                    throw new InvalidOperationException(string.Format("A token with ID '{0}' was not found.", asString));

                // update...
                token.UpdateExpiration();

                // set...
                this.Token = token;
            }
        }
예제 #2
0
        public void TestGetReportsByUser()
        {
            ResetReports();

            // create some reports..
            User user = this.Creator.CreateUser();
            Report report1 = this.Creator.CreateReport(user);
            Report report2 = this.Creator.CreateReport(user);
            Report report3 = this.Creator.CreateReport(user);
            Report report4 = this.Creator.CreateReport(user);
            Report report5 = this.Creator.CreateReport(user);

            // get...
            HandleGetReportsByUser handler = new HandleGetReportsByUser();
            JsonData output = new JsonData();
            handler.DoRequest(this.CreateJsonData(user), output);

            // check...
            string asString = output.GetValueSafe<string>("reports");
            IList reports = (IList)new JavaScriptSerializer().DeserializeObject(asString);
            Assert.AreEqual(5, reports.Count);

            // check...
            Assert.AreEqual(this.ApiKey, ((IDictionary)reports[0])["apiKey"]);
            Assert.AreEqual(user.IdAsString, ((IDictionary)reports[0])["ownerUserId"]);
            Assert.AreEqual(this.ApiKey, ((IDictionary)reports[1])["apiKey"]);
            Assert.AreEqual(user.IdAsString, ((IDictionary)reports[1])["ownerUserId"]);
            Assert.AreEqual(this.ApiKey, ((IDictionary)reports[2])["apiKey"]);
            Assert.AreEqual(user.IdAsString, ((IDictionary)reports[2])["ownerUserId"]);
            Assert.AreEqual(this.ApiKey, ((IDictionary)reports[3])["apiKey"]);
            Assert.AreEqual(user.IdAsString, ((IDictionary)reports[3])["ownerUserId"]);
            Assert.AreEqual(this.ApiKey, ((IDictionary)reports[4])["apiKey"]);
            Assert.AreEqual(user.IdAsString, ((IDictionary)reports[4])["ownerUserId"]);
        }
예제 #3
0
        void buttonEnsureSampleData_Click(object sender, EventArgs e)
        {
            this.labelMessage.Text = string.Empty;

            // get...
            string apiKey = this.textApiKey.Text.Trim();
            if (string.IsNullOrEmpty(apiKey))
            {
                this.labelMessage.Text = "You must enter the API key.";
                return;
            }
            string username = this.textUsername.Text.Trim();
            if (string.IsNullOrEmpty(username))
            {
                this.labelMessage.Text = "You must enter the username.";
                return;
            }

            // load the api...
            Entities.ApiUser api = Entities.ApiUser.GetOrCreateApiUser(new Guid(apiKey));
            if(api == null)
                throw new InvalidOperationException("'api' is null.");

            // get the user...
            Entities.User user = Entities.User.GetByUsername(api, username);
            if(user == null)
            {
                this.labelMessage.Text = string.Format("The user '{0}' could not be found.", username);
                return;
            }

            // input...
            JsonData input = new JsonData();
            input["apiKey"] = apiKey;
            input["logonToken"] = Entities.Token.CreateToken(api, user).TheToken;

            // create...
            HandleEnsureTestReports handler = new HandleEnsureTestReports();
            JsonData output = new JsonData();
            handler.DoRequest(input, output);

            // ok...
            if (output.GetValueSafe<bool>("isOk"))
                this.labelMessage.Text = string.Format("Sample data is available for '{0}'.", username);
            else
                this.labelMessage.Text = output.GetValueSafe<string>("error");
        }
        protected override void DoRequest(AjaxContext context, JsonData input, JsonData output)
        {
            // get...
            string name = input.GetValueSafe<string>("name");

            // return...
            output["result"] = string.Format("Hello, {0}.", name);
        }
예제 #5
0
        public void TestHelloWorld()
        {
            JsonData input = new JsonData();
            ApplyApiKey(input);
            input["name"] = "Martha";

            // run...
            HelloWorldHandler handler = new HelloWorldHandler();
            JsonData output = new JsonData();
            handler.DoRequest(input, output);

            // dump...
            string result = output.GetValueSafe<string>("result");
            Console.WriteLine(result);
            Assert.AreEqual("Hello, Martha.", result);
        }
예제 #6
0
        public void TestEnsureTestReports()
        {
            ResetReports();

            // create some reports..
            User user = this.Creator.CreateUser();

            // check...
            Assert.AreEqual(0, user.GetReports(this).Count<Report>());

            // get...
            HandleEnsureTestReports handler = new HandleEnsureTestReports();
            JsonData output = new JsonData();
            handler.DoRequest(this.CreateJsonData(user), output);

            // check...
            string asString = output.GetValueSafe<string>("reports");
            IList reports = (IList)new JavaScriptSerializer().DeserializeObject(asString);
            Assert.AreEqual(50, reports.Count);
        }
예제 #7
0
        private object GetRequiredValue(JsonData input, string name)
        {
            object value = input.GetValueSafe(name);
            if(value == null || (value is string && ((string)value).Length == 0))
                this.Missings.Add(name);

            return value;
        }