Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion App/lib/services/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class ApiService {
// no auth, we remove the token and return null;
// TODO get a new access_token if the refresh is still valid
_account.remove(Account.token);
DI.authentication?.logOut();
DI.authentication.logOut();
result = null;
break;
default:
Expand Down
1 change: 0 additions & 1 deletion App/lib/services/swagger/generated_code/client_index.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export 'swagger.swagger.dart' show Swagger;
export 'swagger.swagger.dart' show Swagger;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:collection/collection.dart';

enum MetricDataType {
@JsonValue(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:chopper/chopper.dart';
import 'client_mapping.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:http/http.dart' show MultipartFile;
import 'package:chopper/chopper.dart' as chopper;
import 'swagger.enums.swagger.dart' as enums;
export 'swagger.enums.swagger.dart';
Expand Down
2 changes: 0 additions & 2 deletions App/lib/ui/blocs/common/date_input.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:helse/helpers/date.dart';

Expand Down
1 change: 0 additions & 1 deletion App/lib/ui/home.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:helse/logic/d_i.dart';

import 'admin_dashboard.dart';
import '../helpers/date.dart';
import '../services/swagger/generated_code/swagger.swagger.dart';
import 'administration.dart';
Expand Down
7 changes: 4 additions & 3 deletions Backend/Api/Helpers/RightsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Security.Claims;
using Api.Data;
using Api.Data.Models;
using Api.Helpers.Auth;
Expand All @@ -23,7 +24,7 @@ internal static async Task<bool> ValidateCaregiverAsync(this IUserContext db, Us
return right is not null;
}

internal static async Task<IResult?> IsAdmin(this IUserContext db, HttpContext context)
internal static async Task<IResult?> IsAdmin(this IUserContext db, ClaimsPrincipal context)
{
var (error, user) = await db.GetUser(context);
if (error is not null)
Expand All @@ -35,10 +36,10 @@ internal static async Task<bool> ValidateCaregiverAsync(this IUserContext db, Us
return null;
}

internal static async Task<(IResult?, User)> GetUser(this IUserContext db, HttpContext context)
internal static async Task<(IResult?, User)> GetUser(this IUserContext db, ClaimsPrincipal context)
{
// get the connected user
var userName = context.User.GetUser();
var userName = context.GetUser();

var user = await db.Get(userName);
if (user is null)
Expand Down
12 changes: 6 additions & 6 deletions Backend/Api/Logic/EventsLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class EventsLogic
{
public async static Task<IResult> GetAsync(int type, DateTime start, DateTime end, long? personId, IUserContext users, IHealthContext events, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand All @@ -39,7 +39,7 @@ public async static Task<IResult> GetAsync(int type, DateTime start, DateTime en

public static async Task<IResult> CreateAsync(CreateEvent e, long? personId, IUserContext users, IHealthContext events, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand All @@ -53,7 +53,7 @@ public static async Task<IResult> CreateAsync(CreateEvent e, long? personId, IUs

public async static Task<IResult> DeleteAsync(long id, IUserContext users, IHealthContext events, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand All @@ -78,7 +78,7 @@ public async static Task<IResult> DeleteAsync(long id, IUserContext users, IHeal

public static async Task<IResult> CreateTypeAsync(Data.Models.EventType type, IUserContext users, IHealthContext events, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand All @@ -89,7 +89,7 @@ public static async Task<IResult> CreateTypeAsync(Data.Models.EventType type, IU

public static async Task<IResult> UpdateTypeAsync(Data.Models.EventType type, IUserContext users, IHealthContext events, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand All @@ -100,7 +100,7 @@ public static async Task<IResult> UpdateTypeAsync(Data.Models.EventType type, IU

public async static Task<IResult> DeleteTypeAsync(long id, IUserContext users, IHealthContext events, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand Down
2 changes: 1 addition & 1 deletion Backend/Api/Logic/ImportLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static IResult GetTypeAsync()

public static async Task<IResult> PostFileAsync([FromBody] string file, int type, IUserContext users, IHealthContext db, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand Down
12 changes: 6 additions & 6 deletions Backend/Api/Logic/MetricsLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class MetricsLogic
{
public async static Task<IResult> GetAsync(int type, DateTime start, DateTime end, long? personId, IUserContext users, IHealthContext db, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand All @@ -37,7 +37,7 @@ public async static Task<IResult> GetAsync(int type, DateTime start, DateTime en

public static async Task<IResult> CreateAsync(CreateMetric metric, long? personId, IUserContext users, IHealthContext db, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand All @@ -51,7 +51,7 @@ public static async Task<IResult> CreateAsync(CreateMetric metric, long? personI

public async static Task<IResult> DeleteAsync(long id, IUserContext users, IHealthContext db, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand Down Expand Up @@ -83,7 +83,7 @@ public async static Task<IResult> DeleteAsync(long id, IUserContext users, IHeal

public static async Task<IResult> CreateTypeAsync(Models.MetricType metric, IUserContext users, IHealthContext db, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand All @@ -106,7 +106,7 @@ await db.Insert(new Data.Models.MetricType

public static async Task<IResult> UpdateTypeAsync(Models.MetricType metric, IUserContext users, IHealthContext db, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand All @@ -130,7 +130,7 @@ await db.Update(new Data.Models.MetricType

public async static Task<IResult> DeleteTypeAsync(long id, IUserContext users, IHealthContext db, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand Down
4 changes: 2 additions & 2 deletions Backend/Api/Logic/PatientsLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class PatientsLogic
/// <returns></returns>
public static async Task<IResult> GetPatientsAsync(IUserContext users, IHealthContext db, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand All @@ -40,7 +40,7 @@ public static async Task<IResult> GetPatientsAsync(IUserContext users, IHealthCo

public async static Task<IResult> GetAgendaAsync(DateTime start, DateTime end, IUserContext users, IHealthContext db, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand Down
6 changes: 3 additions & 3 deletions Backend/Api/Logic/PersonLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class PersonLogic
/// <returns></returns>
public static async Task<IResult> GetAsync(IUserContext db, HttpContext context)
{
var admin = await db.IsAdmin(context);
var admin = await db.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand Down Expand Up @@ -71,7 +71,7 @@ public static async Task<IResult> GetAsync(IUserContext db, HttpContext context)
/// <returns></returns>
public static async Task<IResult> SetRight(long personId, List<Models.Right> rights, IUserContext users, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand Down Expand Up @@ -167,7 +167,7 @@ public static async Task<IResult> CreateAsync(Models.PersonCreation newUser, IUs
/// <returns></returns>
public static async Task<IResult> SetPersonRole(long personId, UserType role, IUserContext db, HttpContext context)
{
var admin = await db.IsAdmin(context);
var admin = await db.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand Down
8 changes: 4 additions & 4 deletions Backend/Api/Logic/SettingsLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class SettingsLogic
/// <returns></returns>
public static async Task<IResult> GetOauthAsync(IUserContext users, ISettingsContext settings, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand All @@ -32,7 +32,7 @@ public static async Task<IResult> GetOauthAsync(IUserContext users, ISettingsCon
/// <returns></returns>
public static async Task<IResult> GetProxyAsync(IUserContext users, ISettingsContext settings, HttpContext context)
{
var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand All @@ -50,7 +50,7 @@ public static async Task<IResult> PostOauthAsync(Oauth settings, IUserContext us
{
var log = logger.CreateLogger(nameof(SettingsLogic));

var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand All @@ -72,7 +72,7 @@ public static async Task<IResult> PostProxyAsync(Proxy settings, IUserContext us
{
var log = logger.CreateLogger(nameof(SettingsLogic));

var admin = await users.IsAdmin(context);
var admin = await users.IsAdmin(context.User);
if (admin is not null)
return admin;

Expand Down
4 changes: 2 additions & 2 deletions Backend/Api/Logic/TreatmentLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class TreatmentLogic

public async static Task<IResult> PostAsync(Models.CreateTreatment treatment, IUserContext db, HttpContext context)
{
var (error, user) = await db.GetUser(context);
var (error, user) = await db.GetUser(context.User);
if (error is not null)
return error;

Expand Down Expand Up @@ -49,7 +49,7 @@ public async static Task<IResult> PostAsync(Models.CreateTreatment treatment, IU

public async static Task<IResult> GetAsync(DateTime start, DateTime end, long? personId, IUserContext users, IHealthContext db, HttpContext context)
{
var (error, user) = await users.GetUser(context);
var (error, user) = await users.GetUser(context.User);
if (error is not null)
return error;

Expand Down
63 changes: 63 additions & 0 deletions Backend/Tests/Unit/Logic/MetricLogic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Security.Claims;
using Api.Data;
using Api.Data.Models;
using Api.Logic;
using Api.Models;
using Microsoft.AspNetCore.Http;
using NSubstitute;

public class MetricLogicTests
{
private readonly IUserContext _users = Substitute.For<IUserContext>();
private readonly IHealthContext _db = Substitute.For<IHealthContext>();

[Fact]
public async Task MetricType_AddTextSumAsync()
{
var type = new Api.Models.MetricType()
{
Name = "",
Type = MetricDataType.Text,
SummaryType = MetricSummary.Sum,
};
var context = new DefaultHttpContext
{
User = new System.Security.Claims.ClaimsPrincipal([
new ClaimsIdentity(),
])
};
_users.Get(default).ReturnsForAnyArgs(new PersonFromDb(new User
{
Identifier = "",
Password = "",
Type = 2,
}, new Api.Data.Models.Person()));

await Assert.ThrowsAsync<System.IO.InvalidDataException>(() => MetricsLogic.CreateTypeAsync(type, _users, _db, context));
}

[Fact]
public async Task MetricType_AddTextMeanAsync()
{
var type = new Api.Models.MetricType()
{
Name = "",
Type = MetricDataType.Text,
SummaryType = MetricSummary.Sum,
};
var context = new DefaultHttpContext
{
User = new System.Security.Claims.ClaimsPrincipal([
new ClaimsIdentity(),
])
};
_users.Get(default).ReturnsForAnyArgs(new PersonFromDb(new User
{
Identifier = "",
Password = "",
Type = 2,
}, new Api.Data.Models.Person()));

await Assert.ThrowsAsync<System.IO.InvalidDataException>(() => MetricsLogic.CreateTypeAsync(type, _users, _db, context));
}
}