Ejemplo n.º 1
0
        public void AuthorizeUser()
        {
            try
            {
                ITraktAuthorization      authorization           = _traktClient.GetAuthorization(PinCode);
                ITraktUserSettings       traktUserSettings       = _traktClient.GetTraktUserSettings();
                ITraktSyncLastActivities traktSyncLastActivities = _traktClient.GetLastActivities();

                string traktUserHomePath = _mediaPortalServices.GetTraktUserHomePath();
                if (!_fileOperations.DirectoryExists(traktUserHomePath))
                {
                    _fileOperations.CreateDirectory(traktUserHomePath);
                }

                SaveTraktAuthorization(authorization, traktUserHomePath);
                SaveTraktUserSettings(traktUserSettings, traktUserHomePath);
                SaveLastSyncActivities(traktSyncLastActivities, traktUserHomePath);

                TestStatus       = "[Trakt.AuthorizationSucceed]";
                IsUserAuthorized = true;
            }
            catch (Exception ex)
            {
                TestStatus = "[Trakt.AuthorizationFailed]";
                _mediaPortalServices.GetLogger().Error(ex);
                IsUserAuthorized = false;
            }
        }
Ejemplo n.º 2
0
        private void SaveLibraryMovies(IEnumerable <MediaLibraryMovie> libraryMovies)
        {
            string libraryMoviesPath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.MediaLibraryMovies.Value);
            string libraryMoviesJson = JsonConvert.SerializeObject(libraryMovies);

            _fileOperations.FileWriteAllText(libraryMoviesPath, libraryMoviesJson, Encoding.UTF8);
        }
Ejemplo n.º 3
0
        public void SyncCollectedMoviesToTrakt()
        {
            // Arrange
            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.MarkAsWatched(Arg.Any <MediaItem>()).Returns(true);
            mediaPortalServices.MarkAsUnWatched(Arg.Any <MediaItem>()).Returns(true);
            mediaPortalServices.GetTraktUserHomePath()
            .Returns(HomeUserPath);

            IList <MediaItem>         collectedMovies = new List <MediaItem>();
            IList <MediaLibraryMovie> savedMovies     = new List <MediaLibraryMovie>();
            string collectedMoviesPath = Path.Combine(mediaPortalServices.GetTraktUserHomePath(), FileName.MediaLibraryMovies.Value);

            if (File.Exists(collectedMoviesPath))
            {
                string collectedMoviesJson = File.ReadAllText(collectedMoviesPath);
                savedMovies = JsonConvert.DeserializeObject <List <MediaLibraryMovie> >(collectedMoviesJson);
            }

            foreach (MediaLibraryMovie movie in savedMovies)
            {
                collectedMovies.Add(new MockedDatabaseMovie(new MediaLibraryMovie
                {
                    Title      = movie.Title,
                    Imdb       = movie.Imdb,
                    Year       = movie.Year,
                    AddedToDb  = movie.AddedToDb,
                    LastPlayed = movie.LastPlayed,
                    PlayCount  = movie.PlayCount
                }).Movie);
            }

            IContentDirectory contentDirectory = Substitute.For <IContentDirectory>();

            contentDirectory.SearchAsync(Arg.Any <MediaItemQuery>(), true, null, false).Returns(collectedMovies);
            mediaPortalServices.GetServerConnectionManager().ContentDirectory.Returns(contentDirectory);

            ILogger      logger      = Substitute.For <ILogger>();
            ITraktClient traktClient = new TraktClientProxy(ApplicationId, SecretId, logger);


            ValidateAuthorization(traktClient, new FileOperations());

            IFileOperations         fileOperations         = new FileOperations();
            ITraktCache             traktCache             = new TraktCache(mediaPortalServices, traktClient, fileOperations);
            ILibrarySynchronization librarySynchronization = new LibrarySynchronization(mediaPortalServices, traktClient, traktCache, fileOperations);

            // Act
            TraktSyncMoviesResult result = librarySynchronization.SyncMovies();

            // Assert
            Assert.NotNull(result);
        }
Ejemplo n.º 4
0
        public void ShouldFailToAuthorizeUserWhenTraktAuthorizationThrowsException()
        {
            // Arrange
            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.GetTraktUserHomePath().Returns(DataPath);
            ITraktClient traktClient = Substitute.For <ITraktClient>();

            traktClient.GetAuthorization(Arg.Any <string>()).Throws(new TraktException("Pin code is not valid"));
            ILibrarySynchronization librarySynchronization = Substitute.For <ILibrarySynchronization>();
            IFileOperations         fileOperations         = Substitute.For <IFileOperations>();

            fileOperations.DirectoryExists(DataPath).Returns(true);
            string expectedStatus = "[Trakt.AuthorizationFailed]";

            TraktSetupModel setupModel = new TraktSetupModel(mediaPortalServices, traktClient, librarySynchronization, fileOperations);

            // Act
            setupModel.AuthorizeUser();
            string actualStatus = setupModel.TestStatus;

            // Assert
            Assert.Equal(expectedStatus, actualStatus);
            Assert.False(setupModel.IsUserAuthorized);
        }
Ejemplo n.º 5
0
        public void GetCollectedMovies(List <ITraktCollectionMovie> onlineCollectedMovies, ITraktSyncLastActivities onlineLastSyncActivities, int expectedCollectedMoviesCount)
        {
            // Arrange
            ITraktClient traktClient = Substitute.For <ITraktClient>();

            traktClient.GetCollectedMovies().Returns(onlineCollectedMovies);
            traktClient.GetLastActivities().Returns(onlineLastSyncActivities);

            IFileOperations fileOperations = Substitute.For <IFileOperations>();

            SetFileOperationsForFile(fileOperations, DataPath, FileName.LastActivity.Value);
            SetFileOperationsForFile(fileOperations, DataPath, FileName.CollectedMovies.Value);
            SetFileOperationsForFile(fileOperations, DataPath, FileName.WatchedMovies.Value);

            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.GetTraktUserHomePath().Returns(DataPath);

            // Act
            ITraktCache traktCache  = new TraktCache(mediaPortalServices, traktClient, fileOperations);
            TraktMovies traktMovies = traktCache.RefreshMoviesCache();

            // Assert
            int actualCollectedMoviesCount = traktMovies.Collected.Count();

            Assert.Equal(expectedCollectedMoviesCount, actualCollectedMoviesCount);
        }
        public void StartScrobble(TraktPluginSettings settings, MediaItem mediaItem, ITraktClient traktClient, ITraktNotification notification)
        {
            // Arrange
            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.GetTraktUserHomePath().Returns(DataPath);
            SetSettings(mediaPortalServices, settings);
            SetPlayerAndContentDirForMovie(mediaPortalServices, mediaItem);

            IAsynchronousMessageQueue messageQueue = GetMockedMsgQueue(mediaPortalServices);

            IFileOperations fileOperations = Substitute.For <IFileOperations>();

            fileOperations.FileExists(Path.Combine(DataPath, FileName.Authorization.Value)).Returns(true);

            TraktScrobbleHandlerManager      traktScrobbleHandler = new TraktScrobbleHandlerManager(mediaPortalServices, traktClient, fileOperations);
            TraktScrobbleStartedNotification expectedNotification = (TraktScrobbleStartedNotification)notification;

            // Act
            // start the player
            messageQueue.MessageReceivedProxy += Raise.Event <MessageReceivedHandler>(new AsynchronousMessageQueue(new object(), new[] { "PlayerManager" }),
                                                                                      GetSystemMessageForMessageType(PlayerManagerMessaging.MessageType.PlayerStarted));

            // Assert
            mediaPortalServices.GetTraktNotificationModel().Received()
            .ShowNotification(Arg.Is <TraktScrobbleStartedNotification>(x => x.IsSuccess == expectedNotification.IsSuccess &&
                                                                        x.Message == expectedNotification.Message &&
                                                                        x.ActionType == expectedNotification.ActionType &&
                                                                        x.Progress == expectedNotification.Progress &&
                                                                        x.SuperLayerScreenName == expectedNotification.SuperLayerScreenName),
                              Arg.Any <TimeSpan>());
        }
        public void EnableTraktScrobbleHandlerWhenUserChanged()
        {
            // Arrange
            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.GetTraktUserHomePath().Returns(DataPath);
            TraktPluginSettings settings = new TraktPluginSettings
            {
                IsScrobbleEnabled = false
            };
            ITraktSettingsChangeWatcher settingsChangeWatcher = Substitute.For <ITraktSettingsChangeWatcher>();

            settingsChangeWatcher.TraktSettings.Returns(settings);
            mediaPortalServices.GetTraktSettingsWatcher().Returns(settingsChangeWatcher);

            IUserMessageHandler userMessageHandler = Substitute.For <IUserMessageHandler>();

            mediaPortalServices.GetUserMessageHandler().Returns(userMessageHandler);

            ITraktClient    traktClient    = Substitute.For <ITraktClient>();
            IFileOperations fileOperations = Substitute.For <IFileOperations>();

            fileOperations.FileExists(Path.Combine(DataPath, FileName.Authorization.Value)).Returns(true);

            TraktScrobbleHandlerManager traktScrobbleHandler = new TraktScrobbleHandlerManager(mediaPortalServices, traktClient, fileOperations);

            // Act
            settings.IsScrobbleEnabled           = true;
            userMessageHandler.UserChangedProxy += Raise.Event();

            // Assert
            Assert.True(traktScrobbleHandler.IsActive);
        }
Ejemplo n.º 8
0
        public void ShouldSyncLibraryWhenShareImportCompletedIsWithSuccess()
        {
            // Arrange
            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.GetTraktUserHomePath().Returns(DataPath);
            SetSettings(mediaPortalServices, new TraktPluginSettings {
                IsAutomaticLibrarySyncEnabled = true, ShowAutomaticSyncNotifications = true
            });

            IAsynchronousMessageQueue messageQueue = GetMockedMsgQueue(mediaPortalServices);

            IFileOperations fileOperations = Substitute.For <IFileOperations>();

            fileOperations.FileExists(Path.Combine(DataPath, FileName.Authorization.Value)).Returns(true);

            ILibrarySynchronization librarySynchronization = Substitute.For <ILibrarySynchronization>();

            librarySynchronization.SyncMovies().Returns(new TraktSyncMoviesResult());
            librarySynchronization.SyncSeries().Returns(new TraktSyncEpisodesResult());

            TraktSyncHandlerManager traktScrobbleHandler = new TraktSyncHandlerManager(mediaPortalServices, librarySynchronization, fileOperations);

            // Act
            // send share import completed message
            messageQueue.MessageReceivedProxy += Raise.Event <MessageReceivedHandler>(new AsynchronousMessageQueue(new object(), new[] { "ContentDirectory" }),
                                                                                      GetSystemMessageForMessageType(ContentDirectoryMessaging.MessageType.ShareImportCompleted));

            // Assert
            mediaPortalServices.GetTraktNotificationModel().Received().ShowNotification(Arg.Any <TraktSyncLibrarySuccessNotification>(), Arg.Any <TimeSpan>());
        }
Ejemplo n.º 9
0
        public void ShouldEnableTraktSyncHandlerWhenSettingsChange()
        {
            // Arrange
            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.GetTraktUserHomePath().Returns(DataPath);
            TraktPluginSettings settings = new TraktPluginSettings
            {
                IsAutomaticLibrarySyncEnabled = false
            };
            ITraktSettingsChangeWatcher settingsChangeWatcher = Substitute.For <ITraktSettingsChangeWatcher>();

            settingsChangeWatcher.TraktSettings.Returns(settings);
            mediaPortalServices.GetTraktSettingsWatcher().Returns(settingsChangeWatcher);

            ISettingsManager settingsManager = Substitute.For <ISettingsManager>();

            settingsManager.Load <TraktPluginSettings>().Returns(settings);
            mediaPortalServices.GetSettingsManager().Returns(settingsManager);

            ILibrarySynchronization librarySynchronization = Substitute.For <ILibrarySynchronization>();
            IFileOperations         fileOperations         = Substitute.For <IFileOperations>();

            fileOperations.FileExists(Path.Combine(DataPath, FileName.Authorization.Value)).Returns(true);

            TraktSyncHandlerManager traktSyncHandler = new TraktSyncHandlerManager(mediaPortalServices, librarySynchronization, fileOperations);

            // Act
            settings.IsAutomaticLibrarySyncEnabled      = true;
            settingsChangeWatcher.TraktSettingsChanged += Raise.Event();

            // Assert
            Assert.True(traktSyncHandler.IsActive);
        }
Ejemplo n.º 10
0
        private void ConfigureHandler()
        {
            string authorizationFilePath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.Authorization.Value);
            bool   isUserAuthorized      = _fileOperations.FileExists(authorizationFilePath);
            bool   isScrobbleEnabled     = _mediaPortalServices.GetTraktSettingsWatcher().TraktSettings.IsScrobbleEnabled;

            if (isUserAuthorized && isScrobbleEnabled)
            {
                SubscribeToMessages();
                IsActive = true;
                _mediaPortalServices.GetLogger().Info("Trakt: enabled trakt scrobble handler.");
            }
            else
            {
                UnsubscribeFromMessages();
                IsActive = false;
                _mediaPortalServices.GetLogger().Info("Trakt: disabled trakt scrobble handler.");
            }
        }
Ejemplo n.º 11
0
        public void ShouldAuthorizeUserForValidData()
        {
            // Arrange
            IMediaPortalServices mediaPortalServices = Substitute.For <IMediaPortalServices>();

            mediaPortalServices.GetTraktUserHomePath().Returns(DataPath);
            ITraktClient            traktClient            = Substitute.For <ITraktClient>();
            ILibrarySynchronization librarySynchronization = Substitute.For <ILibrarySynchronization>();
            IFileOperations         fileOperations         = Substitute.For <IFileOperations>();

            fileOperations.DirectoryExists(DataPath).Returns(true);
            string expectedStatus = "[Trakt.AuthorizationSucceed]";

            TraktSetupModel setupModel = new TraktSetupModel(mediaPortalServices, traktClient, librarySynchronization, fileOperations);

            // Act
            setupModel.AuthorizeUser();
            string actualStatus = setupModel.TestStatus;

            // Assert
            Assert.Equal(expectedStatus, actualStatus);
            Assert.True(setupModel.IsUserAuthorized);
        }
Ejemplo n.º 12
0
        private bool IsCacheInitialized(string file)
        {
            string filePath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), file);

            return(_fileOperations.FileExists(filePath));
        }