public void GetOrCreateRegistrationWhenRegistrationAlreadyExistsTest() { var expectedRegistration = new Registration { ID = 3, ClientId = "client-id", ClientSecret = "client-secret", Instance = "mastodon.instance", MastodonAppRegistrationID = "42" }; _mockData.Setup(d => d.GetAppRegistration("mastodon.instance")).Returns(expectedRegistration); var herdApp = new HerdApp(_mockData.Object, _mockHashTagRelevanceManager.Object, _mockMastodonApiWrapper.Object, _mockLogger.Object); var result = herdApp.GetOrCreateRegistration(new GetOrCreateRegistrationCommand { Instance = "mastodon.instance" }); // Verify the result Assert.True(result?.Success); ExtendedAssert.ObjectsEqual(expectedRegistration, result.Data.Registration); // Make sure the GetAppRegistration was called once and CreateAppRegistration was never called _mockData.Verify(d => d.GetAppRegistration("mastodon.instance"), Times.Once()); _mockData.Verify(d => d.CreateAppRegistration(It.Is <Registration>(r => true)), Times.Never()); }
public void GetOrCreateRegistrationWhenRegistrationDoesNotAlreadyExistTest() { _mockData.Setup(d => d.GetAppRegistration("mastodon.instance")).Returns(null as Registration); _mockMastodonApiWrapper.Setup(a => a.RegisterApp()).Returns(Task.FromResult(new Registration { ClientId = "client-id", ClientSecret = "client-secret", Instance = "mastodon.instance", MastodonAppRegistrationID = "42", ID = -1 })); _mockData.Setup(d => d.CreateAppRegistration(It.Is <Registration>(r => r.Instance == "mastodon.instance"))) .Returns <Registration>(registrationToCreate => registrationToCreate.With(r => r.ID = 3)); var herdApp = new HerdApp(_mockData.Object, _mockHashTagRelevanceManager.Object, _mockMastodonApiWrapper.Object, _mockLogger.Object); var result = herdApp.GetOrCreateRegistration(new GetOrCreateRegistrationCommand { Instance = "mastodon.instance" }); // Verify the result var expectedRegistration = new Registration { ID = 3, ClientId = "client-id", ClientSecret = "client-secret", Instance = "mastodon.instance", MastodonAppRegistrationID = "42" }; Assert.True(result?.Success); ExtendedAssert.ObjectsEqual(expectedRegistration, result.Data.Registration); // Make sure the GetAppRegistration and CreateAppRegistration were each called exactly once _mockData.Verify(d => d.GetAppRegistration("mastodon.instance"), Times.Once()); _mockMastodonApiWrapper.Verify(a => a.RegisterApp(), Times.Once()); _mockData.Verify(d => d.CreateAppRegistration(It.Is <Registration>(r => r.Instance == "mastodon.instance")), Times.Once()); }