public void DeleteKeyInDatabase() { var apiKey = new IndicoApiKey() { Site = "full moon", ApiKey = "1234", SecretKey = "5678" }; IndicoApiKeyAccess.UpdateKey(apiKey); var vm = new AddOrUpdateIndicoApiKeyViewModel(apiKey); var init1 = vm.AddOrUpdateText; bool canExeAdd = false; vm.AddUpdateCommand.CanExecuteObservable.Subscribe(v => canExeAdd = v); bool canExeDelete = false; vm.DeleteCommand.CanExecuteObservable.Subscribe(v => canExeDelete = v); Assert.IsTrue(canExeDelete); Assert.IsNotNull(IndicoApiKeyAccess.GetKey("full moon")); vm.DeleteCommand.Execute(null); Assert.IsNull(IndicoApiKeyAccess.GetKey("full moon")); Assert.IsFalse(canExeDelete); Assert.AreEqual("Add", vm.AddOrUpdateText); }
public void CreateWithExitingApiKey() { var apiKey = new IndicoApiKey() { Site = "full moon", ApiKey = "1234", SecretKey = "5678" }; IndicoApiKeyAccess.UpdateKey(apiKey); var vm = new AddOrUpdateIndicoApiKeyViewModel(apiKey); var init1 = vm.AddOrUpdateText; bool canExeAdd = false; vm.AddUpdateCommand.CanExecuteObservable.Subscribe(v => canExeAdd = v); bool canExeDelete = false; vm.DeleteCommand.CanExecuteObservable.Subscribe(v => canExeDelete = v); Assert.AreEqual("full moon", vm.SiteName); Assert.AreEqual("5678", vm.SecretKey); Assert.AreEqual("1234", vm.ApiKey); Assert.AreEqual("Update", vm.AddOrUpdateText); Assert.IsTrue(canExeAdd); Assert.IsTrue(canExeDelete); }
public void NewKeyEnteredIntoDatabase() { var vm = new AddOrUpdateIndicoApiKeyViewModel(null); var init1 = vm.AddOrUpdateText; bool canExeAdd = false; vm.AddUpdateCommand.CanExecuteObservable.Subscribe(v => canExeAdd = v); bool canExeDelete = false; vm.DeleteCommand.CanExecuteObservable.Subscribe(v => canExeDelete = v); vm.SiteName = "full moon"; Assert.IsFalse(canExeAdd); vm.SecretKey = "1234"; Assert.IsFalse(canExeAdd); vm.ApiKey = "5678"; Assert.IsTrue(canExeAdd); vm.AddUpdateCommand.Execute(null); var o = IndicoApiKeyAccess.GetKey("full moon"); Assert.IsNotNull(o); Assert.AreEqual("full moon", o.Site); Assert.AreEqual("1234", o.SecretKey); Assert.AreEqual("5678", o.ApiKey); }
public void LoadMissingApiKey() { int count = 0; IndicoApiKeyAccess.IndicoApiKeysUpdated.Subscribe(x => count++); var k = IndicoApiKeyAccess.GetKey("dummy"); Assert.IsNull(k); Assert.AreEqual(0, count); }
public void LoadAllKeysWith0() { int count = 0; IndicoApiKeyAccess.IndicoApiKeysUpdated.Subscribe(x => count++); var keys = IndicoApiKeyAccess.LoadAllKeys(); Assert.AreEqual(0, keys.Length); Assert.AreEqual(0, count); }
public void RemoveAllKeysDontTouchOthers() { ApplicationData.Current.RoamingSettings.Values["bogus"] = "yo dude"; var k = new IndicoApiKey() { ApiKey = "key", SecretKey = "noway", Site = "indico.cern.ch" }; IndicoApiKeyAccess.UpdateKey(k); IndicoApiKeyAccess.RemoveAllKeys(); Assert.IsTrue(ApplicationData.Current.RoamingSettings.Values.ContainsKey("bogus")); ApplicationData.Current.RoamingSettings.Values.Remove("bogus"); }
public void LoadAllKeysWith2() { var k = new IndicoApiKey() { ApiKey = "key", SecretKey = "noway", Site = "indico.cern.ch" }; IndicoApiKeyAccess.UpdateKey(k); k.ApiKey = "key2"; k.Site = "indico.fnal.gov"; IndicoApiKeyAccess.UpdateKey(k); var keys = IndicoApiKeyAccess.LoadAllKeys(); Assert.AreEqual(2, keys.Length); }
/// <summary> /// Create displaying everything. Having a key as null is fine - then everything /// starts out blank. /// </summary> /// <param name="key"></param> public AddOrUpdateIndicoApiKeyViewModel(IndicoApiKey key) { if (key == null) { SiteName = ""; ApiKey = ""; SecretKey = ""; } else { SiteName = key.Site; ApiKey = key.ApiKey; SecretKey = key.SecretKey; } // Setup the add and update commands to work correctly. // A missing secret key is allowed. var addCanExe = this.WhenAny(x => x.SiteName, x => x.ApiKey, x => x.SecretKey, (site, apik, seck) => Tuple.Create(site.Value, apik.Value, seck.Value)) .Select(x => !string.IsNullOrWhiteSpace(x.Item1) && !string.IsNullOrWhiteSpace(x.Item2)); AddUpdateCommand = ReactiveCommand.Create(addCanExe); var isKnownSite = new ReplaySubject <bool>(1); DeleteCommand = ReactiveCommand.Create(isKnownSite.StartWith(false)); Observable.Merge( this.WhenAny(x => x.SiteName, x => x.Value), AddUpdateCommand.IsExecuting.Where(isexe => isexe == false).Select(_ => SiteName), DeleteCommand.IsExecuting.Where(isexe => isexe == false).Select(_ => SiteName) ) .Select(sname => IndicoApiKeyAccess.GetKey(sname) != null) .Subscribe(gotit => isKnownSite.OnNext(gotit)); isKnownSite .Select(known => known ? "Update" : "Add") .ToProperty(this, x => x.AddOrUpdateText, out _addOrUpdateText, "Add"); // Add, update, or remove AddUpdateCommand .Subscribe(o => IndicoApiKeyAccess.UpdateKey(new IndicoApiKey() { Site = SiteName, ApiKey = ApiKey, SecretKey = SecretKey })); DeleteCommand .Subscribe(o => IndicoApiKeyAccess.RemoveKey(SiteName)); }
public void RemoveAllKeys() { int count = 0; IndicoApiKeyAccess.IndicoApiKeysUpdated.Subscribe(x => count++); var k = new IndicoApiKey() { ApiKey = "key", SecretKey = "noway", Site = "indico.cern.ch" }; IndicoApiKeyAccess.UpdateKey(k); IndicoApiKeyAccess.RemoveAllKeys(); var fk = IndicoApiKeyAccess.GetKey("indico.cern.ch"); Assert.IsNull(fk); Assert.AreEqual(2, count); }
public void UpdateExistingKey() { var k = new IndicoApiKey() { ApiKey = "key", SecretKey = "noway", Site = "indico.cern.ch" }; IndicoApiKeyAccess.UpdateKey(k); k.ApiKey = "bogus"; IndicoApiKeyAccess.UpdateKey(k); var fk = IndicoApiKeyAccess.GetKey("indico.cern.ch"); Assert.IsNotNull(fk); Assert.AreEqual("bogus", fk.ApiKey); Assert.AreEqual("noway", fk.SecretKey); Assert.AreEqual("indico.cern.ch", fk.Site); }
public void LoadPresentApi2KeysSecond() { var k = new IndicoApiKey() { ApiKey = "key", SecretKey = "noway", Site = "indico.cern.ch" }; IndicoApiKeyAccess.UpdateKey(k); k.ApiKey = "key2"; k.Site = "indico.fnal.gov"; IndicoApiKeyAccess.UpdateKey(k); var fk = IndicoApiKeyAccess.GetKey("indico.fnal.gov"); Assert.IsNotNull(fk); Assert.AreEqual("key2", fk.ApiKey); Assert.AreEqual("noway", fk.SecretKey); Assert.AreEqual("indico.fnal.gov", fk.Site); }
public void LoadPresentApiKey() { int count = 0; IndicoApiKeyAccess.IndicoApiKeysUpdated.Subscribe(x => count++); var k = new IndicoApiKey() { ApiKey = "key", SecretKey = "noway", Site = "indico.cern.ch" }; IndicoApiKeyAccess.UpdateKey(k); var fk = IndicoApiKeyAccess.GetKey("indico.cern.ch"); Assert.IsNotNull(fk); Assert.AreEqual("key", fk.ApiKey); Assert.AreEqual("noway", fk.SecretKey); Assert.AreEqual("indico.cern.ch", fk.Site); Assert.AreEqual(1, count); }
/// <summary> /// Basic settings are configured on the view that is attached to this VM. /// </summary> /// <param name="screen"></param> public BasicSettingsViewModel(IScreen screen) { HostScreen = screen; // Look for a currently loaded cert and update the status... // We can't start this b.c. the ToProperty is lazy - and it won't // fire until Status is data-bound! LookupCertStatus = ReactiveCommand.CreateAsyncTask(a => SecurityUtils.FindCert(SecurityUtils.CERNCertName)); LookupCertStatus .Select(c => c == null ? "No Cert Loaded" : string.Format("Loaded (expires {0})", c.ValidTo.ToLocalTime().ToString("yyyy-MM-dd HH:mm"))) .ToProperty(this, x => x.Status, out _status, "", RxApp.MainThreadScheduler); LookupCertStatus .ExecuteAsync() .Subscribe(); // Error and status messages... var errors = new Subject <string>(); errors .ToProperty(this, x => x.Error, out _error, "", RxApp.MainThreadScheduler); // Given a file and a password, see if we can install it as a cert // in our internal repository. LoadFiles = ReactiveCommand.Create(); LoadFiles .Subscribe(x => errors.OnNext("")); var files = LoadFiles .Cast <Tuple <IReadOnlyList <StorageFile>, string> >(); files .Where(finfo => finfo.Item1 == null || finfo.Item1.Count != 1) .Select(f => "Invalid certificate file") .Subscribe(errors); files .Where(finfo => finfo.Item1 != null && finfo.Item1.Count == 1) .ObserveOn(RxApp.MainThreadScheduler) .Select(mf => { // We use this double subscribe because the readBufferAsync and ImportPfxDataAsync often return exceptions. // If we let the exception bubble all the way up, it terminates the sequence. Which means if the user entered // the wrong password they wouldn't get a chance to try again! return(Observable.Return(mf) .SelectMany(async f => { // Work around for the TplEventListener not working correctly. // https://social.msdn.microsoft.com/Forums/windowsapps/en-US/3e505e04-7f30-4313-aa47-275eaef333dd/systemargumentexception-use-of-undefined-keyword-value-1-for-event-taskscheduled-in-async?forum=wpdevelop await Task.Delay(1); var fs = f.Item1[0] as StorageFile; var buffer = await FileIO.ReadBufferAsync(fs); var cert = CryptographicBuffer.EncodeToBase64String(buffer); await CertificateEnrollmentManager.ImportPfxDataAsync(cert, f.Item2, ExportOption.NotExportable, KeyProtectionLevel.NoConsent, InstallOptions.DeleteExpired, SecurityUtils.CERNCertName); return Unit.Default; })); }) .Subscribe(c => c.Subscribe( g => LookupCertStatus.ExecuteAsync().Subscribe(), e => errors.OnNext(e.Message.TakeFirstLine()) )); // Set/Get the file expiration policy. CacheDecayOptions = ExpirationOptions.GetListExpirationOptions(); // Get the list of indico api keys we are watching // and hook up the MV for doing the api key manipulation ApiKeysForIndico = new ReactiveList <IndicoApiKey>(); ApiKeysForIndico.AddRange(IndicoApiKeyAccess.LoadAllKeys()); IndicoApiKeyAccess.IndicoApiKeysUpdated .Subscribe(_ => { using (ApiKeysForIndico.SuppressChangeNotifications()) { ApiKeysForIndico.Clear(); ApiKeysForIndico.AddRange(IndicoApiKeyAccess.LoadAllKeys()); } }); ShowIndicoApiKey = ReactiveCommand.Create(); ShowIndicoApiKey .Cast <IndicoApiKey>() .Select(x => new AddOrUpdateIndicoApiKeyViewModel(x)) .ToProperty(this, x => x.IndicoApiKey, out _indicoApiKeyVM, new AddOrUpdateIndicoApiKeyViewModel(null)); }
public void ResetApiKeyStore() { IndicoApiKeyAccess.RemoveAllKeys(); }