Example #1
0
        public void TestUserSignOn()
        {
            Wammer.Cloud.UserLogInResponse res =
                new Wammer.Cloud.UserLogInResponse(200, DateTime.Now.ToUniversalTime(), "token1");
            res.user = new Wammer.Cloud.UserInfo {
                user_id = "uid"
            };

            using (FakeCloud fakeCloud = new FakeCloud(res))
                using (WebClient agent = new WebClient())
                {
                    Wammer.Cloud.User user = Wammer.Cloud.User.LogIn(agent, "user1", "passwd1");
                    Assert.AreEqual("user1", user.Name);
                    Assert.AreEqual("passwd1", user.Password);
                    Assert.AreEqual("uid", user.Id);
                    Assert.AreEqual("/v9999/auth/login",
                                    fakeCloud.RequestedPath);
                    Assert.AreEqual("email=user1&password=passwd1&apikey=apiKey1",
                                    fakeCloud.PostData);
                    Assert.AreEqual("application/x-www-form-urlencoded",
                                    fakeCloud.RequestedContentType);

                    Assert.AreEqual("token1", user.Token);
                }
        }
Example #2
0
        public void TestStationLogOnParams()
        {
            using (FakeCloud fakeCloud = new FakeCloud(new Wammer.Cloud.StationLogOnResponse(200, DateTime.Now.ToUniversalTime(), "newToken1")))
                using (WebClient agent = new WebClient())
                {
                    Wammer.Cloud.StationApi api = new Wammer.Cloud.StationApi("sid1", "token1");

                    Dictionary <object, object> param = new Dictionary <object, object>();
                    param.Add("host_name", "hostname1");
                    param.Add("ip_address", "ip1");
                    param.Add("port", "9999");
                    api.LogOn(agent, param);

                    Assert.AreEqual("/v9999/stations/logOn",
                                    fakeCloud.RequestedPath);
                    Assert.AreEqual(
                        "host_name=hostname1&ip_address=ip1&port=9999&" +
                        "session_token=token1&station_id=sid1&apikey=apiKey1",
                        fakeCloud.PostData);
                    Assert.AreEqual("application/x-www-form-urlencoded",
                                    fakeCloud.RequestedContentType);
                    Assert.AreEqual("newToken1", api.Token);

                    Assert.AreEqual("newToken1", api.Token);
                }
        }
        public void TestAddTheSameDriver()
        {
            UserLogInResponse res = new UserLogInResponse
            {
                api_ret_message = "success",
                api_ret_code    = 0,
                session_token   = "token2",
                status          = 200,
                timestamp       = DateTime.UtcNow,
                groups          = new List <UserGroup> {
                    new UserGroup {
                        creator_id  = "creator1",
                        description = "gdesc1",
                        group_id    = "group_id1",
                        name        = "group1"
                    }
                },
                user = new UserInfo {
                    user_id = "uid1"
                }
            };

            using (FakeCloud cloud = new FakeCloud(res))
            {
                CloudServer.request <CloudResponse>(new WebClient(), REST_COMMAND_ADD,
                                                    new Dictionary <object, object> {
                    { "email", "*****@*****.**" },
                    { "password", "12345" }
                });
            }

            Driver driver = Wammer.Model.DriverCollection.Instance.FindOne(Query.EQ("email", "*****@*****.**"));

            Assert.IsNotNull(driver);
        }
        public void TestRemoveDriver_RemoveWhenSingleItem()
        {
            mongodb.GetDatabase("wammer").GetCollection <Driver>("drivers").RemoveAll();
            mongodb.GetDatabase("wammer").GetCollection("station").RemoveAll();

            mongodb.GetDatabase("wammer").GetCollection <Driver>("drivers").Insert(
                new Driver
            {
                user_id = "exist_uid1",
                email   = "*****@*****.**",
                folder  = "resource\\user_exist_uid1",
                groups  = new List <UserGroup> {
                    new UserGroup {
                        group_id = "123"
                    }
                }
            });

            mongodb.GetDatabase("wammer").GetCollection("station").Insert(
                new StationInfo()
            {
                Id = "1234"
            });

            using (FakeCloud cloud = new FakeCloud(new CloudResponse()))
            {
                CloudServer.request <CloudResponse>(new WebClient(), "http://localhost:8080/v2/station/drivers/remove",
                                                    new Dictionary <object, object> {
                    { "session_token", "token123" },
                    { "user_ID", "exist_uid1" }
                });
                Assert.IsNull(Wammer.Model.StationCollection.Instance.FindOne());
            }
        }
Example #5
0
        public void TestObjectReceiveHandler_UploadNewSmallImage()
        {
            using (FakeCloud cloud = new FakeCloud(cloudResponse))
                using (HttpServer server = new HttpServer(8080))
                {
                    server.AddHandler("/test/", new AttachmentUploadHandler());
                    server.Start();

                    ObjectUploadResponse res = null;
                    using (FileStream input = File.OpenRead("Penguins.jpg"))
                        using (MemoryStream output = new MemoryStream())
                        {
                            input.CopyTo(output);
                            res = Attachment.UploadImage("http://*****:*****@"resource\group1\" + res.object_id + ".jpg"));

                            using (Bitmap largeImg = new Bitmap(@"resource\group1\" + res.object_id + "_large.jpg"))
                            {
                                Assert.AreEqual(1024, largeImg.Width);
                                Assert.AreEqual(768, largeImg.Height);
                            }
                        }
                }
        }
        public void TestAddADriver_secStation_usingStationController()
        {
            StationSignUpResponse res1 = new StationSignUpResponse
            {
                api_ret_code    = 0,
                api_ret_message = "success",
                session_token   = "token1",
                status          = 200,
                timestamp       = DateTime.UtcNow
            };

            UserLogInResponse res2 = new UserLogInResponse
            {
                api_ret_message = "success",
                api_ret_code    = 0,
                session_token   = "token2",
                status          = 200,
                timestamp       = DateTime.UtcNow,
                groups          = new List <UserGroup> {
                    new UserGroup {
                        creator_id  = "creator1",
                        description = "gdesc1",
                        group_id    = "group_id1",
                        name        = "group1"
                    }
                },
                user = new UserInfo {
                    user_id = "uid1"
                },
                stations = new List <UserStation>()
                {
                    new UserStation()
                    {
                        station_id = "aabbcc"
                    },
                }
            };

            StationLogOnResponse res3 = new StationLogOnResponse(200, DateTime.UtcNow, "token3");

            res3.api_ret_code = 0;

            using (FakeCloud cloud = new FakeCloud(res1))
            {
                cloud.addJsonResponse(res3);
                cloud.addJsonResponse(res2);

                StationController.StationMgmtURL = "http://127.0.0.1:8080/v2/";
                AddUserResult result = StationController.AddUser("*****@*****.**", "123456");
                Assert.AreEqual("uid1", result.UserId);
                Assert.IsFalse(result.IsPrimaryStation);
            }
        }
Example #7
0
 public void TestStationLogOn()
 {
     using (FakeCloud fakeCloud = new FakeCloud(new Wammer.Cloud.StationLogOnResponse(200, DateTime.Now.ToUniversalTime(), "newToken1")))
         using (WebClient agent = new WebClient())
         {
             Wammer.Cloud.StationApi api = new Wammer.Cloud.StationApi("sid1", "token1");
             api.LogOn(agent);
             Assert.AreEqual("/v9999/stations/logOn",
                             fakeCloud.RequestedPath);
             Assert.AreEqual("session_token=token1&station_id=sid1&apikey=apiKey1",
                             fakeCloud.PostData);
             Assert.AreEqual("application/x-www-form-urlencoded",
                             fakeCloud.RequestedContentType);
             Assert.AreEqual("newToken1", api.Token);
         }
 }
        public void TestView_FileNotFound_ForwardToCloud_ByPost()
        {
            CloudServer.BaseUrl = "http://localhost/v2/";

            RawDataResponseWriter writer = new RawDataResponseWriter
            {
                RawData     = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 },
                ContentType = "image/jpeg"
            };

            using (FakeCloud cloud = new FakeCloud(writer))
                using (HttpServer server = new HttpServer(8080))
                {
                    server.AddHandler("/v1/objects/view", new AttachmentViewHandler("sid"));
                    server.Start();

                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
                        "http://localhost:8080/v1/objects/view");
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Method      = "POST";
                    using (StreamWriter fs = new StreamWriter(req.GetRequestStream()))
                    {
                        fs.Write("object_id=abc&apikey=123&session_token=token123&image_meta=medium");
                    }

                    HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                    Assert.AreEqual("image/jpeg", response.ContentType);

                    using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
                    {
                        byte[] readData = reader.ReadBytes(1000);
                        Assert.AreEqual(writer.RawData.Length, readData.Length);

                        for (int i = 0; i < readData.Length; i++)
                        {
                            Assert.AreEqual(writer.RawData[i], readData[i]);
                        }
                    }
                }
        }
Example #9
0
        public void TestUserSignOnError()
        {
            Wammer.Cloud.UserLogInResponse res =
                new Wammer.Cloud.UserLogInResponse(403,
                                                   DateTime.Now.ToUniversalTime(), "token1");
            res.api_ret_code = 9999;

            using (FakeCloud fakeCloud = new FakeCloud(res))
                using (WebClient agent = new WebClient())
                {
                    try
                    {
                        Wammer.Cloud.User.LogIn(agent, "user1", "passwd1");
                    }
                    catch (Wammer.Cloud.WammerCloudException e)
                    {
                        Assert.AreEqual(res.api_ret_code, e.WammerError);
                        return;
                    }
                    Assert.Fail("Expected exception is not thrown.");
                }
        }
Example #10
0
        public void TestStationLogOnParams_Encoding()
        {
            using (FakeCloud fakeCloud = new FakeCloud(new Wammer.Cloud.StationLogOnResponse(200, DateTime.Now.ToUniversalTime(), "newToken1")))
                using (WebClient agent = new WebClient())
                {
                    Wammer.Cloud.StationApi api = new Wammer.Cloud.StationApi("sid1", "token1");

                    Dictionary <object, object> param = new Dictionary <object, object>();
                    param.Add("key", @"<>+@/\|");
                    api.LogOn(agent, param);

                    Assert.AreEqual("/v9999/stations/logOn",
                                    fakeCloud.RequestedPath);
                    Assert.AreEqual(
                        "key=" + HttpUtility.UrlEncode(@"<>+@/\|") +
                        "&session_token=token1&station_id=sid1&apikey=apiKey1",
                        fakeCloud.PostData);
                    Assert.AreEqual("application/x-www-form-urlencoded",
                                    fakeCloud.RequestedContentType);
                    Assert.AreEqual("newToken1", api.Token);

                    Assert.AreEqual("newToken1", api.Token);
                }
        }
        public void TestAddADriver_incorrectUserNamePwd()
        {
            UserLogInResponse res1 = new UserLogInResponse
            {
                api_ret_message = "station res msg",
                api_ret_code    = 4097,              // cloud retuns 4097 for invalid user name or password
                session_token   = "token1",
                status          = (int)HttpStatusCode.Forbidden,
                timestamp       = DateTime.UtcNow,
                groups          = new List <UserGroup>(),
                user            = new UserInfo {
                    user_id = "uid1"
                }
            };

            using (FakeCloud cloud = new FakeCloud(res1, res1.status))
            {
                try
                {
                    CloudServer.request <CloudResponse>(
                        new WebClient(),
                        "http://*****:*****@gmail.com" },
                        { "password", "12345" }
                    });
                }
                catch (WammerCloudException e)
                {
                    Assert.AreEqual((int)StationApiError.AuthFailed, e.WammerError);
                    return;
                }

                Assert.Fail("Expected exception is not thrown");
            }
        }
        public void TestAddADriver()
        {
            StationSignUpResponse res1 = new StationSignUpResponse
            {
                api_ret_code    = 0,
                api_ret_message = "success",
                session_token   = "token1",
                status          = 200,
                timestamp       = DateTime.UtcNow
            };

            UserLogInResponse res2 = new UserLogInResponse
            {
                api_ret_message = "success",
                api_ret_code    = 0,
                session_token   = "token2",
                status          = 200,
                timestamp       = DateTime.UtcNow,
                groups          = new List <UserGroup> {
                    new UserGroup {
                        creator_id  = "creator1",
                        description = "gdesc1",
                        group_id    = "group_id1",
                        name        = "group1"
                    }
                },
                user = new UserInfo {
                    user_id = "uid1"
                },
                stations = new List <UserStation>()
                {
                    new UserStation()
                    {
                        station_id = "stationId", type = "primary"
                    }
                }
            };

            StationLogOnResponse res3 = new StationLogOnResponse(200, DateTime.UtcNow, "token3");

            res3.api_ret_code = 0;

            using (FakeCloud cloud = new FakeCloud(res1))
            {
                cloud.addJsonResponse(res3);
                cloud.addJsonResponse(res2);
                CloudServer.request <CloudResponse>(new WebClient(), "http://*****:*****@gmail.com" },
                    { "password", "12345" }
                });


                // verify db
                Driver driver = mongodb.GetDatabase("wammer").
                                GetCollection <Driver>("drivers").FindOne(
                    Query.EQ("email", "*****@*****.**"));

                Assert.AreEqual("*****@*****.**", driver.email);
                Assert.AreEqual(@"resource\user_uid1", driver.folder);
                Assert.AreEqual(res2.user.user_id, driver.user_id);
                Assert.IsTrue(driver.isPrimaryStation);
                Assert.AreEqual(1, driver.groups.Count);
                Assert.AreEqual(res2.session_token, driver.session_token);
                Assert.AreEqual(res2.groups[0].group_id, driver.groups[0].group_id);
                Assert.AreEqual(res2.groups[0].name, driver.groups[0].name);
                Assert.AreEqual(res2.groups[0].description, driver.groups[0].description);

                //verify station
                Wammer.Model.StationInfo s = Wammer.Model.StationCollection.Instance.FindOne();
                Assert.IsNotNull(s);
                Assert.AreEqual("token3", s.SessionToken);
            }
        }