/// <summary>
        /// Add a user to a station
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns>station's session token</returns>
        /// <exception cref="Wammer.Station.Management.AuthenticationException">
        /// Invalid user name or password
        /// </exception>
        /// <exception cref="Wammer.Station.Management.StationAlreadyHasDriverException">
        /// The station already has an driver
        /// </exception>
        /// <exception cref="Wammer.Station.Management.UserAlreadyHasStationException">
        /// The user already has a station. The station's info, such as id/location/sync time, can
        /// be retrieved from the exception
        /// </exception>
        /// <exception cref="Wammer.Station.Management.StationServiceDownException">
        /// Unable to connect to station service, service down?
        /// </exception>
        /// <exception cref="Wammer.Station.Management.ConnectToCloudException">
        /// Unable to connect to waveface cloud, network down?
        /// </exception>
        public static AddUserResult AddUser(string email, string password)
        {
            try
            {
                AddUserResponse res = CloudServer.request <AddUserResponse>(
                    new WebClient(),
                    StationMgmtURL + "station/drivers/add",
                    new Dictionary <object, object> {
                    { "email", email },
                    { "password", password }
                });

                return(new AddUserResult()
                {
                    UserId = res.UserId, IsPrimaryStation = res.IsPrimaryStation
                });
            }
            catch (Cloud.WammerCloudException e)
            {
                if (e.HttpError == WebExceptionStatus.ConnectFailure)
                {
                    throw new StationServiceDownException("Station service down?");
                }

                switch (e.WammerError)
                {
                case (int)StationApiError.ConnectToCloudError:
                    throw new ConnectToCloudException(e.Message);

                case (int)StationApiError.AuthFailed:
                    throw new AuthenticationException(e.Message);

                case (int)StationApiError.DriverExist:
                    throw new StationAlreadyHasDriverException(e.Message);

                case (int)StationApiError.AlreadyHasStaion:
                    StationSignUpResponse resp = fastJSON.JSON.Instance.
                                                 ToObject <Cloud.StationSignUpResponse>(e.response);
                    throw new UserAlreadyHasStationException
                          {
                              Id           = resp.station.station_id,
                              Location     = resp.station.location,
                              LastSyncTime = resp.station.LastSeen,
                              ComputerName = resp.station.computer_name
                          };

                case 0x4000 + 4:                         // user not exist
                    throw new AuthenticationException(e.Message);

                default:
                    throw;
                }
            }
        }
Example #2
0
        public void TestStationSignUpResponse()
        {
            StationSignUpResponse res = fastJSON.JSON.Instance.ToObject <StationSignUpResponse>(
                "{\"status\": 200, \"timestamp\": \"2011-01-01T10:20:30Z\"," +
                "\"session_token\": \"token1\", " +
                "\"station\": {\"station_id\": \"sid1\", \"creator_id\": \"cid1\", \"timestamp\": \"2011-10-20T10:20:30Z\", \"name\": \"name1\"} }");

            Assert.AreEqual(200, res.status);
            Assert.AreEqual(new DateTime(2011, 1, 1, 10, 20, 30, DateTimeKind.Utc), res.timestamp.ToUniversalTime());
            Assert.AreEqual("token1", res.session_token);
        }
        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);
            }
        }
        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);
            }
        }