コード例 #1
0
        public async void ActivatePluginAsync_on_plugin_never_activated()
        {
            // Given no meta record exist for MyPlugin
            var key = "myplugin";

            metaRepoMock.Setup(repo => repo.GetAsync(key, EMetaType.Plugin)).Returns(Task.FromResult((Meta)null));
            // suppose user activates MyPlugin, mock an active plugin to be returned by repo create
            var myPlugin = new MyPlugin
            {
                Active = true
            };
            var meta = new Meta {
                Id = 1, Key = key, Value = JsonConvert.SerializeObject(myPlugin), Type = EMetaType.Plugin
            };

            metaRepoMock.Setup(repo => repo.CreateAsync(It.Is <Meta>(m => m.Key == key))).Returns(Task.FromResult(meta));
            metaRepoMock.Setup(repo => repo.GetAsync(1)).Returns(Task.FromResult(meta));

            // When user activates it
            var plugin = await pluginService.ActivatePluginAsync(myPlugin.Folder);

            // Then
            // repo create is called once
            metaRepoMock.Verify(repo => repo.CreateAsync(It.IsAny <Meta>()), Times.Once);
            // the returned plugin is active
            Assert.Equal(1, plugin.Id);
            Assert.True(plugin.Active);
        }
コード例 #2
0
        public async void GetActivePluginsAsync_returns_all_active_plugins_and_activates_any_new_plugins()
        {
            // Arrange
            // an active plugin in db
            var key      = "myplugin";
            var myPlugin = new MyPlugin {
                Active = true
            };
            var meta = new Meta {
                Id = 1, Key = key, Value = JsonConvert.SerializeObject(myPlugin), Type = EMetaType.Plugin
            };
            IEnumerable <Meta> activePluginMetas = new List <Meta> {
                meta
            };

            metaRepoMock.Setup(repo => repo.FindAsync(m => m.Type == EMetaType.Plugin)).Returns(Task.FromResult(activePluginMetas));

            myPlugin.Active = true;
            var metaActive = new Meta {
                Id = 1, Key = key, Value = JsonConvert.SerializeObject(myPlugin), Type = EMetaType.Plugin
            };

            metaRepoMock.Setup(repo => repo.GetAsync(1)).Returns(Task.FromResult(metaActive));

            // suppose a sys plugin is copied over then mock its meta to be returned
            var key2        = "mysysplugin";
            var mySysPlugin = new MySysPlugin();
            var meta2       = new Meta {
                Id = 2, Key = key2, Value = JsonConvert.SerializeObject(mySysPlugin), Type = EMetaType.Plugin
            };

            metaRepoMock.Setup(repo => repo.CreateAsync(It.Is <Meta>(m => m.Key == key2))).Returns(Task.FromResult(meta2));

            mySysPlugin.Active = true;
            var meta2Active = new Meta {
                Id = 2, Key = key2, Value = JsonConvert.SerializeObject(mySysPlugin), Type = EMetaType.Plugin
            };

            metaRepoMock.Setup(repo => repo.GetAsync(2)).Returns(Task.FromResult(meta2Active));

            // Act
            var activePlugins = await pluginService.GetActivePluginsAsync();

            // Assert
            Assert.Equal(2, activePlugins.Count());

            var mySysPluginAgain = activePlugins.ToList()[0];
            var myPluginAgain    = activePlugins.ToList()[1];

            // plugins are of their specific types
            Assert.True(mySysPluginAgain.GetType() == typeof(MySysPlugin));
            Assert.True(myPluginAgain.GetType() == typeof(MyPlugin));

            // plugins are active
            Assert.True(mySysPluginAgain.Active);
            Assert.True(myPluginAgain.Active);

            Assert.Equal(mySysPlugin.Folder, mySysPluginAgain.Folder);
            Assert.Equal(myPlugin.Folder, myPluginAgain.Folder);
        }
コード例 #3
0
        public async void UpsertPluginAsync_updates_inactive_plugin_settings()
        {
            // Given an in-active plugin on the Plugins page
            var key          = "myplugin";
            var value        = @"{""age"":15,""name"":""John"",""folder"":""MyPlugin""}";
            var metaCreating = new Meta {
                Key = key, Value = value, Type = EMetaType.Plugin
            };
            var metaCreated = new Meta {
                Id = 1, Key = key, Value = value, Type = EMetaType.Plugin
            };

            metaRepoMock.Setup(repo => repo.GetAsync(key, EMetaType.Plugin)).Returns(Task.FromResult(metaCreated));

            // When user updates my plugin
            var myPlugin = new MyPlugin {
                Name = "John", Folder = "MyPlugin"
            };
            var id = await pluginService.UpsertPluginAsync(myPlugin);

            // Then plugin is not active but the settings have been updated
            Assert.Equal(1, id);
            metaRepoMock.Verify(repo => repo.CreateAsync(It.IsAny <Meta>()), Times.Never);
            metaRepoMock.Verify(repo => repo.UpdateAsync(It.IsAny <Meta>()), Times.Once);
        }
コード例 #4
0
        public async void ActivatePluginAsync_on_plugin_that_is_not_active()
        {
            // Arrange a plugin meta
            var key      = "myplugin";
            var myPlugin = new MyPlugin();
            var meta     = new Meta {
                Id = 1, Key = key, Value = JsonConvert.SerializeObject(myPlugin), Type = EMetaType.Plugin
            };

            metaRepoMock.Setup(repo => repo.GetAsync(key, EMetaType.Plugin)).Returns(Task.FromResult(meta));
            metaRepoMock.Setup(repo => repo.GetAsync(1)).Returns(Task.FromResult(meta));

            // Act when user activates it
            var plugin = await pluginService.ActivatePluginAsync(myPlugin.Folder);

            // Assert the plugin is updated at datasource
            metaRepoMock.Verify(repo => repo.UpdateAsync(It.IsAny <Meta>()), Times.Once);
        }
コード例 #5
0
        public async void DeactivatePluginAsync_turns_plugin_active_to_false()
        {
            // Arrange a plugin meta
            var key      = "myplugin";
            var myPlugin = new MyPlugin {
                Active = true
            };
            var meta = new Meta {
                Id = 1, Key = key, Value = JsonConvert.SerializeObject(myPlugin), Type = EMetaType.Plugin
            };

            metaRepoMock.Setup(repo => repo.GetAsync(key, EMetaType.Plugin)).Returns(Task.FromResult(meta));
            metaRepoMock.Setup(repo => repo.GetAsync(1)).Returns(Task.FromResult(meta));

            // Act when user de-activates it
            await pluginService.DeactivatePluginAsync(1);

            // Assert the plugin is updated at datasource
            metaRepoMock.Verify(repo => repo.UpdateAsync(It.IsAny <Meta>()), Times.Once);
        }