예제 #1
0
        public void TestQueryDetail()
        {
            var authRepo = MockManager.MockAll <AuthRepository>(Constructor.Mocked);

            using (var repository = new FakeFlickrRepository <PhotoRepository, Photo>())
            {
                authRepo.ExpectAndReturn("Authenticate", "1234", 1).Args(true, Permission.Delete);

                repository.FakeElementCall("Linq.Flickr.Test.Responses.PhotoDetail.xml");

                var photoDetailQuery = from photo in context.Photos
                                       where photo.Id == "xxx" && photo.PhotoSize == PhotoSize.Medium && photo.ViewMode == ViewMode.Owner
                                       select photo;

                Photo detailPhoto = photoDetailQuery.Single();
                // Element test
                Assert.IsTrue(detailPhoto.Title == "Mug Shot");
                // more that 1
                Assert.IsTrue(detailPhoto.Tags.Length > 1);

                Assert.IsTrue(detailPhoto.UploadedOn == GetDate("1208716675"));
                Assert.IsTrue(detailPhoto.User == "*Park+Ride*");
                Assert.IsTrue(detailPhoto.NsId == "63497523@N00");
                Assert.IsTrue(detailPhoto.WebUrl == "http://www.flickr.com/photos/63497523@N00/2428052817/");
            }
            authRepo.Verify();
        }
예제 #2
0
        public void GetResponseRedirectsExceededTest()
        {
            HttpWebServiceRequest  request     = new HttpWebServiceRequest(_webServiceState);
            Mock <HttpWebResponse> webResponse = MockManager.MockAll <HttpWebResponse>(Constructor.NotMocked);

            webResponse.ExpectGetAlways("StatusCode", HttpStatusCode.Redirect);
            webResponse.ExpectAndReturn("GetResponseHeader", _url, _webServiceState.MaxRedirects + 1);
            try
            {
                request.GetResponse(_url, _responseStream, _accept);
                Assert.Fail("No exception thrown");
            }
            catch (HttpWebServiceException ex)
            {
                Assert.AreEqual(HttpWebServiceExceptionStatus.RedirectsExceeded, ex.Status, "Incorrect exception type");
            }
        }
예제 #3
0
        public void DoPhotoUploadAndDeleteTest()
        {
            MockManager.Init();

            Stream photoRes = GetResourceStream("Linq.Flickr.Test.blank.gif");
            Photo  photo    = new Photo {
                Title = "Flickr logo", FileName = "Test.Mock", File = photoRes, ViewMode = ViewMode.Public
            };

            using (var photoAddMock = new FakeFlickrRepository <PhotoRepository, Photo>())
            {
                var authRepo = MockManager.MockAll <AuthRepository>(Constructor.Mocked);

                authRepo.ExpectAndReturn("Authenticate", "1234", 2).Args(Permission.Delete);


                photoAddMock.FakeSignatureCall();
                photoAddMock.FakeElementCall(ResourceNs + ".UploadStatus.xml");

                byte[] oImage = new byte[photoRes.Length];

                photoRes.Read(oImage, 0, oImage.Length);
                photoRes.Seek(0, SeekOrigin.Begin);

                string path = System.AppDomain.CurrentDomain.BaseDirectory + "\\photo.txt";

                FileStream fileStream = null;

                if (!File.Exists(path))
                {
                    fileStream = File.Create(path);
                }
                else
                {
                    fileStream = File.Open(path, FileMode.Truncate);
                }

                photoAddMock.FakeHttpRequestObject(fileStream);
                photoAddMock.FakeWebResponseGetResponse();
                photoAddMock.FakeWebResponseObject(ResourceNs + ".Photo.xml");

                // add to the collection.
                context.Photos.Add(photo);
                context.Photos.SubmitChanges();

                fileStream.Dispose();

                FileStream readStream = File.Open(path, FileMode.Open);

                // read the binary content from file.
                BinaryReader reader = new BinaryReader(readStream);

                byte[] content = new byte[reader.BaseStream.Length];

                content = reader.ReadBytes(content.Length);

                reader.Close();

                readStream.Close();
                readStream.Dispose();

                // end file read

                // construct and verify image
                byte[] uImage = new byte[oImage.Length];

                byte[] footer = Encoding.UTF8.GetBytes("\r\n--FLICKR_BOUNDARY--\r\n");

                int endIndex   = content.Length - footer.Length;
                int startIndex = endIndex - oImage.Length;

                int count = 0;

                for (int index = startIndex; index < endIndex; index++)
                {
                    uImage[count] = content[index];
                    count++;
                }
                MemoryStream mStream = new MemoryStream();

                mStream.Write(uImage, 0, uImage.Length);
                mStream.Seek(0, SeekOrigin.Begin);

                using (Bitmap bitmap = new Bitmap(mStream))
                {
                }

                mStream.Close();
                // end image verification.

                Assert.IsTrue(photo.Id == "1");

                authRepo.Verify();
            }

            using (var photoDeleteMock = new FakeFlickrRepository <PhotoRepository, Photo>())
            {
                var authRepo = MockManager.MockAll <AuthRepository>(Constructor.Mocked);

                authRepo.ExpectAndReturn("Authenticate", "1234", 1).Args(Permission.Delete);

                photoDeleteMock.FakeSignatureCall();
                photoDeleteMock.FakeDoHttpPost(ResourceNs + ".DeletePhoto.xml");

                context.Photos.Remove(photo);
                context.SubmitChanges();

                authRepo.Verify();
            }
        }
예제 #4
0
        public void DoPhoto_CommentTest()
        {
            MockManager.Init();

            Comment comment = new Comment();


            #region Add comment

            var addRepo = MockManager.MockAll <AuthRepository>(Constructor.Mocked);
            addRepo.ExpectAndReturn("Authenticate", "1234", 1).Args(Permission.Delete);

            using (var commentAddMock = new FakeFlickrRepository <CommentRepository, Comment>())
            {
                commentAddMock.FakeDoHttpPostAndReturnStringResult(ResourceNs + ".AddComment.xml");

                comment.PhotoId = COMMENT_PHOTO_ID;
                comment.Text    = "Testing comment add [LINQ.Flickr]";

                context.Photos.Comments.Add(comment);
                context.SubmitChanges();

                Assert.IsTrue(comment.Id == "1");
            }

            addRepo.Verify();
            #endregion

            #region Get added comment



            Comment commentGet = null;

            using (var commentGetMock = new FakeFlickrRepository <CommentRepository, Comment>())
            {
                commentGetMock.FakeSignatureCall();
                commentGetMock.MockRESTBuilderGetElement(ResourceNs + ".GetComment.xml");

                var query = from c in context.Photos.Comments
                            where c.PhotoId == COMMENT_PHOTO_ID && c.Id == comment.Id
                            select c;

                commentGet = query.Single();

                Assert.IsTrue(commentGet.Author == "11" && commentGet.PhotoId == COMMENT_PHOTO_ID && commentGet.AuthorName == "John Doe");
            }

            #endregion


            #region update comment

            var updateRepo = MockManager.MockAll <AuthRepository>(Constructor.Mocked);
            updateRepo.ExpectAndReturn("Authenticate", "1234", 1).Args(Permission.Delete);


            using (var commentUpdateMock = new FakeFlickrRepository <CommentRepository, Comment>())
            {
                const string updatedText = "#123#";
                // line verfies if the text is passed properly for update.
                commentUpdateMock.FakeSignatureCall("flickr.photos.comments.editComment", true, "comment_id", "1", "comment_text", updatedText, "auth_token", "1234");
                commentUpdateMock.FakeDoHttpPostAndReturnStringResult(ResourceNs + ".UpdateComment.xml");

                commentGet.Text = updatedText;

                context.SubmitChanges();
            }

            updateRepo.Verify();

            #endregion

            #region Delete added


            var deleteRepo = MockManager.MockAll <AuthRepository>(Constructor.Mocked);
            deleteRepo.ExpectAndReturn("Authenticate", "1234", 1).Args(Permission.Delete);


            using (var commentDeleteMock = new FakeFlickrRepository <CommentRepository, Comment>())
            {
                commentDeleteMock.FakeSignatureCall();
                commentDeleteMock.FakeDoHttpPostAndReturnStringResult(ResourceNs + ".DeleteComment.xml");

                context.Photos.Comments.Remove(commentGet);
                context.SubmitChanges();
            }

            deleteRepo.Verify();
            #endregion
        }
예제 #5
0
        public void DoPhotoTest()
        {
            MockManager.Init();

            using (var photoGetMock = new FakeFlickrRepository <PhotoRepository, Photo>())
            {
                photoGetMock.FakeElementCall("Linq.Flickr.Test.Responses.Search.xml");

                var searchQuery = (from p in context.Photos
                                   where p.SearchText == "macbook" && p.FilterMode == FilterMode.Safe &&
                                   p.Extras == (ExtrasOption.Views | ExtrasOption.Date_Taken | ExtrasOption.Date_Upload | ExtrasOption.Tags | ExtrasOption.Date_Upload)
                                   orderby PhotoOrder.Interestingness ascending
                                   select p).Take(100);

                int count = searchQuery.Count();

                Photo first = searchQuery.First();

                Assert.IsTrue(first.SharedProperty.Perpage == count);
                Assert.IsTrue(first.Title == "test" && first.Id == "505611561");
                Assert.IsTrue(first.ExtrasResult.Views == 8);

                Photo last = searchQuery.Last();

                Assert.IsTrue(last.SharedProperty.Page == 1);
                Assert.IsTrue(last.SharedProperty.Total == 66604);
                Assert.IsTrue(last.Title == "DSCN0355" && last.Id == "2373030074");

                DateTime dateTime = DateTime.Parse("2008-03-29 20:25:05");

                Assert.IsTrue(dateTime == last.TakeOn);

                dateTime = GetDate("1206847505");

                Assert.IsTrue(dateTime == last.UploadedOn);

                dateTime = GetDate("1206847506");

                Assert.IsTrue(dateTime == last.UpdatedOn);

                Assert.IsTrue(last.Tags.Length == 2);

                //Assert.IsTrue();

                string constructedWebUrl = string.Format("http://www.flickr.com/photos/{0}/{1}/", last.NsId, last.Id);

                Assert.IsTrue(string.Compare(constructedWebUrl, last.WebUrl, StringComparison.Ordinal) == 0);
            }

            Photo authPhoto = null;



            using (var authenticatedMock = new FakeFlickrRepository <PhotoRepository, Photo>())
            {
                var authRepo = MockManager.MockAll <AuthRepository>(Constructor.NotMocked);

                AuthToken token = new AuthToken
                {
                    FullName = "Mehfuz Hossain",
                    Id       = "1",
                    Perm     = "delete",
                    UserId   = "x@y"
                };

                authRepo.ExpectAndReturn("Authenticate", token, 1).Args(true, Permission.Delete);
                ///validates user call
                authenticatedMock.FakeGetNsidByUsername("neetulee");
                authenticatedMock.FakeElementCall("Linq.Flickr.Test.Responses.Owner.xml");

                var authQuery = from photo in context.Photos
                                where photo.ViewMode == ViewMode.Owner && photo.User == "neetulee"
                                select photo;

                authPhoto = authQuery.Last();

                Assert.IsTrue(authPhoto.ViewMode == ViewMode.Private);
                authRepo.Verify();
            }

            using (FakeFlickrRepository <PhotoRepository, Photo> authenticatedMockU = new FakeFlickrRepository <PhotoRepository, Photo>())
            {
                var authRepo = MockManager.MockAll <AuthRepository>(Constructor.Mocked);

                authRepo.ExpectAndReturn("Authenticate", "1234", 1).Args(Permission.Delete);

                const string updatedTitle = "NewTitle";

                // line verfies if the text is passed properly for update.
                authenticatedMockU.FakeSignatureCall("flickr.photos.setMeta", true, "photo_id", authPhoto.Id, "title", updatedTitle,
                                                     "description", authPhoto.Description ?? " ", "auth_token", "1234");

                authenticatedMockU.FakeDoHttpPostAndReturnStringResult(ResourceNs + ".DeletePhoto.xml");

                authPhoto.Title = updatedTitle;
                // will raise a update call.
                context.SubmitChanges();

                authRepo.Verify();
            }
        }