< Summary

Information
Class: Api.Auth.Setup
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Auth/Setup.cs
Line coverage
70%
Covered lines: 28
Uncovered lines: 12
Coverable lines: 40
Total lines: 62
Line coverage: 70%
Branch coverage
12%
Covered branches: 1
Total branches: 8
Branch coverage: 12.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RegisterServices(...)100%11100%
MapEndpoints(...)100%11100%
SeedDb()12.5%11863.64%

File(s)

/home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Auth/Setup.cs

#LineLine coverage
 1// SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5using Api.Auth.Handlers;
 6using Api.Auth.Models;
 7using Api.Auth.Utils;
 8using Api.Database;
 9using Microsoft.AspNetCore.Authorization;
 10using Microsoft.EntityFrameworkCore.Metadata.Internal;
 11
 12namespace Api.Auth;
 13
 14public static class Setup
 15{
 16    public static void RegisterServices(IServiceCollection services)
 117    {
 118        services.AddScoped<ICurrentServiceId, CurrentServiceId>();
 119        services.AddScoped<ICurrentUserId, CurrentUserId>();
 120        services.AddHttpContextAccessor();
 121        services.AddSingleton<IAuthorizationPolicyProvider, PermissionPolicyProvider>();
 122    }
 23
 124    public static void MapEndpoints(IEndpointRouteBuilder route) { }
 25
 26    public static async Task SeedDb(PGContext db, CancellationToken ct)
 127    {
 128        var instanceService = await db.RemoteServices.FindAsync(
 129            [RemoteService.TheInstanceServiceId],
 130            cancellationToken: ct
 131        );
 32
 133        if (instanceService is null)
 134        {
 135            var now = Now();
 136            instanceService = new RemoteService()
 137            {
 138                Id = RemoteService.TheInstanceServiceId,
 139                MetadataAddedAt = now,
 140                MetadataUpdatedAt = now,
 141                IsInstanceService = true,
 142                DefaultInstanceWidePermisssion = UserPermissionBits.All,
 143                Name = RemoteService.TheInstanceServiceName,
 144            };
 145            await db.RemoteServices.AddAsync(instanceService, ct);
 146            await db.SaveChangesAsync(ct);
 147        }
 048        else if (
 049            instanceService.IsInstanceService != true
 050            || instanceService.Name != RemoteService.TheInstanceServiceName
 051            || instanceService.DefaultInstanceWidePermisssion != UserPermissionBits.All
 052        )
 053        {
 054            instanceService.Name = RemoteService.TheInstanceServiceName;
 055            instanceService.IsInstanceService = true;
 056            instanceService.DefaultInstanceWidePermisssion = UserPermissionBits.All;
 057            instanceService.MetadataUpdatedAt = Now();
 58
 059            await db.SaveChangesAsync(ct);
 060        }
 161    }
 62}