Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #36 from getsentry/feat/has-user-on-scope
Browse files Browse the repository at this point in the history
feat: HasUser on scope
  • Loading branch information
bruno-garcia authored May 14, 2019
2 parents 1e6346e + cfec6ee commit 1b1da4e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/Sentry.Protocol/BaseScopeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ namespace Sentry
[EditorBrowsable(EditorBrowsableState.Never)]
public static class BaseScopeExtensions
{
/// <summary>
/// Whether a <see cref="User"/> has been set to the scope with any of its fields non null.
/// </summary>
/// <param name="scope"></param>
/// <returns>True if a User was set to the scope. Otherwise, false.</returns>
public static bool HasUser(this BaseScope scope)
=> scope.InternalUser != null
&& (scope.InternalUser.Email != null
|| scope.InternalUser.Id != null
|| scope.InternalUser.Username != null
|| scope.InternalUser.InternalOther?.Count > 0
|| scope.InternalUser.IpAddress != null);

#if HAS_VALUE_TUPLE
/// <summary>
/// Adds a breadcrumb to the scope
Expand Down
7 changes: 0 additions & 7 deletions src/Sentry.Protocol/SentryEventExtensions.cs

This file was deleted.

71 changes: 71 additions & 0 deletions test/Sentry.Protocol.Tests/BaseScopeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,77 @@ public Fixture()

private readonly Fixture _fixture = new Fixture();

[Fact]
public void HasUser_DefaultScope_ReturnsFalse()
{
var sut = _fixture.GetSut();
Assert.False(sut.HasUser());
}

[Fact]
public void HasUser_NullUser_ReturnsFalse()
{
var sut = _fixture.GetSut();
sut.User = null;
Assert.False(sut.HasUser());
}

[Fact]
public void HasUser_EmptyUser_ReturnsFalse()
{
var sut = _fixture.GetSut();
sut.User = new User();
Assert.False(sut.HasUser());
}

[Fact]
public void HasUser_UserWithId_ReturnsTrue()
{
var sut = _fixture.GetSut();
sut.User.Id = "test";
Assert.True(sut.HasUser());
}

[Fact]
public void HasUser_UserWithUserName_ReturnsTrue()
{
var sut = _fixture.GetSut();
sut.User.Username = "test";
Assert.True(sut.HasUser());
}

[Fact]
public void HasUser_UserWithIpAddress_ReturnsTrue()
{
var sut = _fixture.GetSut();
sut.User.IpAddress = "test";
Assert.True(sut.HasUser());
}

[Fact]
public void HasUser_UserWithEmail_ReturnsTrue()
{
var sut = _fixture.GetSut();
sut.User.Email = "test";
Assert.True(sut.HasUser());
}

[Fact]
public void HasUser_UserWithOther_ReturnsTrue()
{
var sut = _fixture.GetSut();
sut.User.Other.Add("other", "val");
Assert.True(sut.HasUser());
}

[Fact]
public void HasUser_UserWithEmptyOther_ReturnsFalse()
{
var sut = _fixture.GetSut();
sut.User.Other = new Dictionary<string, string>();
Assert.False(sut.HasUser());
}

[Fact]
public void SetFingerprint_NullArgument_ReplacesCurrentWithNull()
{
Expand Down

0 comments on commit 1b1da4e

Please sign in to comment.