aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services.Tests/ProfilePictureService.Tests.cs
blob: a7a2963e25e74bce4f9cf714f37b66153ebcdbe5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.IO;
using System.Threading.Tasks;
using DevHive.Data;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
using DevHive.Data.Repositories;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.ProfilePicture;
using DevHive.Services.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;

namespace DevHive.Services.Tests
{
	[TestFixture]
	public class ProfilePictureServiceTests
	{
		private DevHiveContext _context;
		private Mock<IUserRepository> _userRepositoryMock;
		private Mock<ProfilePictureRepository> _profilePictureRepository;
		private Mock<ICloudService> _cloudService;
		private ProfilePictureService _profilePictureService;

		[SetUp]
		public void Setup()
		{
			DbContextOptionsBuilder<DevHiveContext> options = new DbContextOptionsBuilder<DevHiveContext>()
				.UseInMemoryDatabase("DevHive_UserRepository_Database");
			this._context = new DevHiveContext(options.Options);
			this._userRepositoryMock = new Mock<IUserRepository>();
			this._profilePictureRepository = new Mock<ProfilePictureRepository>(this._context);
			this._cloudService = new Mock<ICloudService>();

			this._profilePictureService = new ProfilePictureService(
				this._userRepositoryMock.Object,
				this._profilePictureRepository.Object,
				this._cloudService.Object
			);
		}

		[Test]
		public async Task InsertProfilePicture_ShouldInsertProfilePicToDatabaseAndUploadToCloud()
		{
			//Arrange
			Guid userId = Guid.NewGuid();
			Mock<IFormFile> fileMock = new();

			//File mocking setup
			var content = "Hello World from a Fake File";
			var fileName = "test.jpg";
			var ms = new MemoryStream();
			var writer = new StreamWriter(ms);
			writer.Write(content);
			writer.Flush();
			ms.Position = 0;
			fileMock.Setup(p => p.FileName).Returns(fileName);
			fileMock.Setup(p => p.Length).Returns(ms.Length);
			fileMock.Setup(p => p.OpenReadStream()).Returns(ms);

			//User Setup
			this._userRepositoryMock
				.Setup(p => p.GetByIdAsync(userId))
				.ReturnsAsync(new User()
				{
					Id = userId
				});

			ProfilePictureServiceModel profilePictureServiceModel = new()
			{
				UserId = userId,
				ProfilePictureFormFile = fileMock.Object
			};

			//Act
			string profilePicURL = await this._profilePictureService.InsertProfilePicture(profilePictureServiceModel);

			//Assert
			Assert.IsNotEmpty(profilePicURL);
		}
	}
}