public void GetEntitiesWithoutLoginOK()
        {
            List <Country> fakeCountries = new List <Country>
            {
                new Country {
                    Iso = "BEL", Iso2 = "BE", Name = "Belgium"
                },
                new Country {
                    Iso = "ITA", Iso2 = "IT", Name = "Italy"
                }
            };

            string content = JsonConvert.SerializeObject(fakeCountries, new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content);

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            List <Country> countries = request.GetEntityList <Country>().GetAwaiter().GetResult();


            UriBuilder uriBuilder  = new UriBuilder("https://api.aproplan.com/rest/countries");
            string     expectedUrl = AproplanApiUtility.BuildRestUrl(request.ApiRootUrl, "countries", request.ApiVersion, request.RequesterId);

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());
            Assert.AreEqual(2, countries.Count);
            Assert.AreEqual(fakeCountries[0].Id, countries[0].Id);
            Assert.AreEqual(fakeCountries[1].Id, countries[1].Id);
            mockWebRequest.Verify();
        }
예제 #2
0
        public void GetEntityByIdOK()
        {
            var  projectId = Guid.NewGuid();
            Note note      = NoteUtility.GetFakeSimpleNotes()[0];

            string content = JsonConvert.SerializeObject(new List <Note> {
                note
            }, new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content);

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            Note resultNote = request.GetEntityById <Note>(note.Id, projectId).GetAwaiter().GetResult();

            string filter = string.Format("Filter.Eq(Id,{0})", note.Id);


            string expectedUrl = AproplanApiUtility.BuildRestUrl(request.ApiRootUrl, "notes", request.ApiVersion, request.RequesterId, request.TokenInfo.Token);

            expectedUrl += "&filter=" + AproplanApiUtility.EncodeUrl(filter) + "&projectid=" + projectId;

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());

            Assert.AreEqual(note.Id, resultNote.Id);

            mockWebRequest.Verify();
        }
예제 #3
0
        public void GetEntityByIdsWithSeveralValOK()
        {
            var         projectId = Guid.NewGuid();
            List <Note> notes     = NoteUtility.GetFakeSimpleNotes();

            string content = JsonConvert.SerializeObject(notes, new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content);

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            Guid[]      ids         = notes.Select((n => n.Id)).ToArray();
            List <Note> resultNotes = request.GetEntityByIds <Note>(ids, projectId).GetAwaiter().GetResult();

            string filter = Filter.In("Id", ids).ToString();


            string expectedUrl = AproplanApiUtility.BuildRestUrl(request.ApiRootUrl, "notes", request.ApiVersion, request.RequesterId, request.TokenInfo.Token);

            expectedUrl += "&filter=" + AproplanApiUtility.EncodeUrl(filter) + "&projectid=" + projectId;

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());

            Assert.AreEqual(notes.Count, resultNotes.Count);
            for (int i = 0; i < notes.Count; i++)
            {
                var expectedNote = notes[i];
                Assert.AreEqual(expectedNote.Id, resultNotes[i].Id);
            }

            mockWebRequest.Verify();
        }
예제 #4
0
        public void LoginWithGoodCredentialsReturnsUser()
        {
            dynamic json = new
            {
                UserInfo      = UserUtility.CreateUser("*****@*****.**", "John Smith"),
                Token         = Guid.NewGuid(),
                ValidityStart = DateTime.Now.ToUniversalTime().ToString("o"),
                ValidityLimit = DateTime.Now.ToUniversalTime().AddMinutes(10).ToString("o")
            };
            string content = JsonConvert.SerializeObject(json);

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content);

            mockWebRequest.SetupSet(r => r.Method      = "POST").Verifiable();
            mockWebRequest.SetupSet(r => r.ContentType = "application/json").Verifiable();

            User       user       = request.Login("*****@*****.**", "aproplan").GetAwaiter().GetResult();
            UriBuilder uriBuilder = new UriBuilder("https://api.aproplan.com/rest/simpleloginsecure");

            string  expectedUrl = AproplanApiUtility.BuildRestUrl(request.ApiRootUrl, "simpleloginsecure", request.ApiVersion, request.RequesterId);
            dynamic requestData = AproplanApiUtility.GetRequestData <ExpandoObject>(mockWebRequest);

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());
            Assert.AreEqual(json.UserInfo.Id, user.Id);
            Assert.AreEqual("*****@*****.**", requestData.alias);
            Assert.AreEqual("aproplan", requestData.pass);
            Assert.AreEqual(json.UserInfo.Id, request.CurrentUser.Id);
            Assert.AreEqual(DateTime.Parse(json.ValidityStart).ToLocalTime(), request.TokenInfo.ValidityStart);
            Assert.AreEqual(DateTime.Parse(json.ValidityLimit).ToLocalTime(), request.TokenInfo.ValidityLimit);

            mockWebRequest.Verify();
        }
예제 #5
0
        public void GetEntityIdsOK()
        {
            var         projectId    = Guid.NewGuid();
            List <Guid> expectedGuid = new List <Guid> {
                Guid.NewGuid(), Guid.NewGuid()
            };

            string content = JsonConvert.SerializeObject(expectedGuid, new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });
            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content);

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            var    id     = Guid.NewGuid();
            Filter filter = Filter.IsNotNull("Subject");

            List <Guid> guids = request.GetEntityIds <Note>(projectId, filter).GetAwaiter().GetResult();

            string expectedUrl = AproplanApiUtility.BuildRestUrl(request.ApiRootUrl, "notesids", request.ApiVersion, request.RequesterId, request.TokenInfo.Token);

            expectedUrl += "&filter=" + AproplanApiUtility.EncodeUrl(filter.ToString()) + "&projectid=" + projectId;

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());
            Assert.AreEqual(expectedGuid.Count, 2);
            Assert.AreEqual(expectedGuid[0], guids[0]);
            Assert.AreEqual(expectedGuid[1], guids[1]);
            mockWebRequest.Verify();
        }
예제 #6
0
        public void RenewTokenValid()
        {
            DateTime currentTokenStart = DateTime.Now.AddMinutes(-6);

            AproplanApiUtility.FakeLogin(request, currentTokenStart);
            Guid    oldToken = request.TokenInfo.Token;
            dynamic json     = new
            {
                Token         = Guid.NewGuid(),
                ValidityStart = DateTime.Now.ToUniversalTime().ToString("o"),
                ValidityLimit = DateTime.Now.ToUniversalTime().AddMinutes(10).ToString("o")
            };
            string content = JsonConvert.SerializeObject(json);

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content);

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            var    tokenInfo   = request.RenewToken().GetAwaiter().GetResult();
            string expectedUrl = AproplanApiUtility.BuildRestUrl(request.ApiRootUrl, "renewtoken", request.ApiVersion, request.RequesterId, oldToken);

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[1].ToString());
            Assert.AreEqual(json.Token, request.TokenInfo.Token);
            Assert.AreEqual(DateTime.Parse(json.ValidityStart).ToLocalTime(), request.TokenInfo.ValidityStart);
            Assert.AreEqual(DateTime.Parse(json.ValidityLimit).ToLocalTime(), request.TokenInfo.ValidityLimit);
        }
        public void SyncProjectsOK()
        {
            List <Project> fakeProjects = ProjectUtility.GetFakeSimpleProjects();

            string content = JsonConvert.SerializeObject(fakeProjects.GetRange(0, 2), new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content, new Dictionary <string, string> {
                { "SyncTimestamp", "stamp1;projectid=3" }
            });

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            SyncResult <Project> result = syncService.SyncProjects(null).GetAwaiter().GetResult();

            UriBuilder uriBuilder = new UriBuilder("https://api.aproplan.com/rest/projectsync");

            string expectedUrl = AproplanApiUtility.BuildRestUrl(mockApi.Object.ApiRootUrl, "projectsync", mockApi.Object.ApiVersion, mockApi.Object.RequesterId);

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());
            Assert.AreEqual(2, result.Data.Count);
            Assert.AreEqual(fakeProjects[0].Id, result.Data[0].Id);
            Assert.AreEqual(fakeProjects[1].Id, result.Data[1].Id);
            Assert.AreEqual("stamp1;projectid=3", result.ContinuationToken);
            mockWebRequest.Verify();
        }
        public void SyncNotesOK()
        {
            Guid        projectId = Guid.NewGuid();
            List <Note> fakeNotes = NoteUtility.GetFakeSimpleNotes();

            string content = JsonConvert.SerializeObject(fakeNotes.GetRange(0, 2), new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content, new Dictionary <string, string> {
                { "SyncTimestamp", "stamp1;noteid=3" }
            });

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            SyncResult <Note> result = syncService.SyncNotes(projectId, null).GetAwaiter().GetResult();


            string expectedUrl = AproplanApiUtility.BuildRestUrl(mockApi.Object.ApiRootUrl, "notesync", mockApi.Object.ApiVersion, mockApi.Object.RequesterId);

            expectedUrl += "&projectid=" + projectId;
            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());
            Assert.AreEqual(2, result.Data.Count);
            Assert.AreEqual(fakeNotes[0].Id, result.Data[0].Id);
            Assert.AreEqual(fakeNotes[1].Id, result.Data[1].Id);
            Assert.AreEqual("stamp1;noteid=3", result.ContinuationToken);
            mockWebRequest.Verify();
        }
예제 #9
0
        public void GetEntityByIdsWithQueryParamsOK()
        {
            var  projectId = Guid.NewGuid();
            Note note      = NoteUtility.GetFakeSimpleNotes()[0];

            string content = JsonConvert.SerializeObject(new List <Note> {
                note
            }, new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content);

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            Guid[]      ids         = new Guid[] { note.Id };
            List <Note> resultNotes = request.GetEntityByIds <Note>(ids, projectId, null, new Dictionary <string, string> {
                { "testparam", "john" }
            }).GetAwaiter().GetResult();

            string filter = Filter.Eq("Id", note.Id).ToString();


            string expectedUrl = AproplanApiUtility.BuildRestUrl(request.ApiRootUrl, "notes", request.ApiVersion, request.RequesterId, request.TokenInfo.Token);

            expectedUrl += "&filter=" + AproplanApiUtility.EncodeUrl(filter) + "&projectid=" + projectId + "&testparam=john";

            Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[0].ToString());

            Assert.AreEqual(1, resultNotes.Count);
            Assert.AreEqual(note.Id, resultNotes[0].Id);


            mockWebRequest.Verify();
        }
        public void SyncAllProjectsOK()
        {
            List <Project> fakeProjects   = ProjectUtility.GetFakeSimpleProjects();
            int            index          = 0;
            List <Project> resultToReturn = fakeProjects.GetRange(index, 2);
            string         content        = JsonConvert.SerializeObject(resultToReturn, new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            string stamp = "stamp" + index + ";projectid=" + index;
            Mock <HttpWebRequest> mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content, new Dictionary <string, string> {
                { "SyncTimestamp", stamp }
            });

            mockWebRequest.SetupSet(r => r.Method = "GET").Verifiable();

            string lastStamp            = stamp;
            string baseUrl              = AproplanApiUtility.BuildRestUrl(mockApi.Object.ApiRootUrl, "projectsync", mockApi.Object.ApiVersion, mockApi.Object.RequesterId);
            SyncResult <Project> result = syncService.SyncAllProjects(null, (SyncResult <Project> r, ref bool cancel) => {
                string expectedUrl = baseUrl;
                if (index > 0)
                {
                    expectedUrl = baseUrl + "&timestamp=" + Uri.EscapeDataString(lastStamp);
                }
                Assert.AreEqual(expectedUrl, FakeWebRequest.Instance.UriCalled[FakeWebRequest.Instance.UriCalled.Count - 1].ToString());
                Assert.AreEqual(stamp, r.ContinuationToken);
                Assert.AreEqual(resultToReturn.Count, r.Data.Count);
                for (var i = 0; i < resultToReturn.Count; i++)
                {
                    Assert.AreEqual(resultToReturn[i].Id, r.Data[i].Id);
                }

                lastStamp = stamp;
                index    += 2;
                int cpt   = fakeProjects.Count - index;

                resultToReturn = cpt > 0 ? fakeProjects.GetRange(index, cpt < 2 ? 1 : 2) : new List <Project>();
                content        = JsonConvert.SerializeObject(resultToReturn, new JsonSerializerSettings
                {
                    DateTimeZoneHandling = DateTimeZoneHandling.Utc
                });
                if (resultToReturn.Count > 0)
                {
                    stamp = "stamp" + index + ";projectid=" + index;
                }
                else
                {
                    stamp = null;
                }
                mockWebRequest = FakeWebRequest.CreateRequestWithResponse(content, new Dictionary <string, string> {
                    { "SyncTimestamp", stamp }
                });
                mockWebRequest.SetupSet(req => req.Method = "GET").Verifiable();
            }).GetAwaiter().GetResult();



            Assert.AreEqual(4, FakeWebRequest.Instance.UriCalled.Count);
            Assert.AreEqual(fakeProjects.Count, result.Data.Count);

            Assert.AreEqual(true, string.IsNullOrEmpty(result.ContinuationToken));

            mockWebRequest.Verify();
        }